5

In a React tutorial video, the instructor said you need to use this.props rather than just props in a React class component because in that case, it's an instance variable. I'm not totally sure what an instance variable is in that context.

I found a lot of questions explaining what an instance variable is in the context of Java and other languages besides JavaScript--in one question (tagged objective-c), someone answered "In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy. They live in memory for the life of the class." However, I know ES6 classes are just objects and are syntactical sugar for prototype-based inheritance --would that answer hold true in the case of JS classes?

1
  • 2
    An instance of a class is just an object. An instance variable is therefore a property of an object. And yes, you can use that term in JavaScript as well. It's the same concept. Commented Apr 3, 2019 at 23:28

3 Answers 3

6

To understand this thing, you need to know about function expression, lexical environment, prototype inheritance.

'this' keyword refers to the object it belongs to.

let say we have a class like this,

class Hello{
   constructor(name){
      this.name = name
   }
   hello(){
      console.log(this.name);
   }
}

now let's make a new object with the help of this class.

var person = new Hello('Alex')

Now I can do something like this,

person.hello();

This line will return 'Alex';

Now let's console.log(person);

console.log(person)

person ==> { 
              name : 'Alex'
              <prototype> {
                  constructor: function Hello()
              }
           }

You will notice that Hello function is not at the top level of the person object. It is at the first prototype level of that object. Everything inside of this prototype will refer to the class or Factory function or Constructor function that helps to define that person object. In this case, it refers to Hello class.

var person is defined on the global level. so when you call to person.hello() it will check at the top level of that object. If it is not in there, then it will go to the outside environment and try to find out what is this hello() thing. The outer environment of this person object is the global level. hello() function is not at the global level. When javaScript did not find the hello() in the outer environment then it will throw an error that saying hello() is undefined.

When we use 'this' keyword, it will say check on prototypes too. go through all the prototypes until you meet that thing. If it is not in there, then go outer environment and check if it is there.

'this' keyword will refer anything, to the object it belongs to. Also, remember everything in javaScript is an object expect primitive data

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, this is helpful, however, a clarifying note for JS newbies--not everything in JS is an object. Aside from objects, there are 6 primitive data types: string, number, boolean, null, undefined, and symbol (new in ES6).
Oh. I wanted to say in JavaScript, functions are objects. Please make me correct if I'm wrong. I think the string/ Boolean are not primitive types. When I type a String or a Boolean value in the console, I can still access to it's proto. So I think String and Boolean types are not actually primitive in javaScript. Yes, I agree with you Numbers, null, undefined are primitive.
For string -- there are both String objects and string literals. The string literals are the primitives stackoverflow.com/questions/7675127/…
Same case for boolean -- there are boolean objects and boolean primitives adripofjavascript.com/blog/drips/…
"Normally JavaScript booleans are primitive values created from literals: var x = false; But booleans can also be defined as objects with the keyword new: var y = new Boolean(false);" w3schools.com/js/js_booleans.asp
|
3

An instance variable is just a property of an object, as Felix Kling said.

You can't use props because that's referencing a global or local variable called props. What you want to access is the current value of props for the current component, stored in this.props.

See a jsfiddle: https://jsfiddle.net/vd5ymhcz/1/

Comments

0

An object is an instance of the class that it is created from. And an instance variable is any property defined in the class for that object (typically inside constructor). Every instance variable is accessible using 'this.' prefix inside any method of the class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.