Javascript function call expression for object member functions

I'm trying to understand at what point a member function of an object becomes associated with an instance of an object. In the following example

function F() {
  this.foo = 'foo';
};

var f = new F();
f.test = function() {
  alert(this.foo);
};

var c = f.test;


f.test();
(f.test)();
var d;
(d = f.test)();
c();    
Run codeHide result

- "foo", "foo", undefined, undefined(as expected). If we look at the AST for the last 4 lines, it looks like this and is the same MemberExpression argument for direct call and destination + call

{
  "type": "MemberExpression",
  "computed": false,
  "object": {
      "type": "Identifier",
      "name": "f"
  },
  "property": {
      "type": "Identifier",
      "name": "test"
  }
}

, , (f.test)() - MemberExpression + CallExpression, MemberCallEcpression ( - )? f.foo ?

+1
1

, / . , , , , . , , , , .

edit β€” RobG , , . , , , , . ( ) , - ., [ ] =. , , , < GetValue. , . [], "this . , , " ", . = () , " ", .

, .

0

Source: https://habr.com/ru/post/1659488/


All Articles