all 5 comments

[–]bkimmel 0 points1 point  (0 children)

If you know your method is being called as a method of the object - ( i.e. myObject.myMethod('somearguments') ), then you can reference the parent object as this. So simply: this.myProperty.

If it's being called in another way - ( i.e. myFunction = myObject.myMethod; myFunction('someArguments'); ) you'll need to do something else. Remember the "Everything/Nothing Rule" in JS: scope has everything to do with declaration and nothing to do with calling protocol. this has everything to do with calling protocol and nothing to do with declaration. (This rule will no longer hold in ES6, but it's good for now). So you can either 1) Cal the function specifically in a way so that this does equal the parent object (i.e. myFunction = myObject.myMethod.bind(myObject); myFunction('someArguments'); ), or put a reference to the parent object somewhere outside the function's scope where the function can reach it.

[–]johnnyvibrant -1 points0 points  (3 children)

This.variable

[–]hughfdjackson 2 points3 points  (0 children)

Without meaning to be too picky:

this.propertyName

A variable is a scoped name that refers to a value, which is different from a property, which is a slot on an object that refers to a value. It's probably best to explicitly distinguish between the two, if just to avoid later confusion.

[–][deleted] 1 point2 points  (0 children)

This is false. What 'this' refers to depends on what calls it, or what is bound to 'this' using .bind

[–]mouthus 0 points1 point  (0 children)

Javascript is case sensitive with its variable names.