Why didn't we use this keyword in Make a New friend while adding an object using array.length?


#1

I was just curious to know why didn't we use this keyword while adding the object in the array in last index.
Ex:

[contacts.length] = {
firstName : firstName,
lastName  : lastName
};

So when we passed into our function these keys as params wouldn't the intrepreter be confused as to which place our value should be like the right or the left? isn't this correct? in that case?

contacts[contacts.length] = {
this.firstName : firstName,
this.lastName  : lastName
};




#2

The above is a plain object literal, which is correct. We only write the this keyword inside constructor function, and inside class methods. Never inside a literal expression.


#3

OMG! Thanks a lot. I totally ignored that its a literal exp! :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.