Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify 'case 2' for 'this' explanation. #2

Closed
wants to merge 1 commit into from
Closed

Clarify 'case 2' for 'this' explanation. #2

wants to merge 1 commit into from

Conversation

kennyklee
Copy link

Here is something I learned today. See example below.

var obj = {
  hello: function() { return "hello, " + this.username; },
  username: "Kenny"
};
obj.hello(); // "hello, Kenny

var obj2 = {
  hello: obj.hello,
  username: "Gordon"
};
obj2. hello(); // "hello, Gordon"

Rather than saying 'contains' the method, 'calls' the method seems more accurate.

Here is something I learned today.  See example below.

```
var obj = {
  hello: function() { return "hello, " + this.username; },
  username: "Kenny"
};
obj.hello(); // "hello, Kenny

var obj2 = {
  hello: obj.hello,
  username: "Gordon"
};
obj2. hello(); // "hello, Gordon"
```

Rather than saying 'contains' the method, 'calls' the method seems more accurate.
@kennyklee
Copy link
Author

I also learned that the object that 'calls the method' is a 'receiver'. I don't think it's worth getting into the vocab...but just found it interesting for my own leveling-up. :)

http://d.weblife.us/Lmyx

@gordonmzhu
Copy link
Owner

I think call is actually more confusing.

Contains is still accurate in your example. When you call obj2.hello, the .hello method belongs to obj2. Or to use my wording, obj2 contains obj2.hello and therefore this.username should be "Gordon".

@gordonmzhu gordonmzhu closed this Nov 25, 2016
@gordonmzhu
Copy link
Owner

@kennyklee I could also be even more explicit and say, this will be whatever is to the left of the .. So for example when you run myObject.myMethod();, myObject is on the left side of ..

Thoughts?

@gordonmzhu gordonmzhu reopened this Nov 25, 2016
@gordonmzhu
Copy link
Owner

@kennyklee "Left of the dot" rule

@gordonmzhu
Copy link
Owner

@kennyklee That's kinda memorable.

@kennyklee
Copy link
Author

@gordonmzhu I see your point. I like the 'left of dot' visual, as long as students understand the bracket notation as well. myObject["myMethod"]()

@gordonmzhu
Copy link
Owner

Closed with ba549ca

@gordonmzhu gordonmzhu closed this Nov 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants