0

Here are the usual constructor function. My question is, how she will lexical environment when calling through new? Namely this.method he gets there or not?

function Func() {
    var a = 10;

    this.method = function() {
        // code
    };

    function f() {

    }
}

Right?

Lexical environment = {
    a: undifined,
    f: function
}

[[Scope]] -> window
Aleksandr
  • 439
  • 3
  • 13
  • 1
    o: the newly-created object. – Teemu Feb 11 '15 at 14:12
  • That is, this.method = function () {// code}; misses the lexical environment? – Aleksandr Feb 11 '15 at 14:17
  • 1
    Yes, to refer to the newly-created object within `method`, you've to assing `this` to a variable in the outer scope. `method` is in the scope of `Func` and the code in `method` is in the scope of that function itself. – Teemu Feb 11 '15 at 14:21
  • @Teemu, look, I have correctly described? http://jsfiddle.net/au7p6deg/ – Aleksandr Feb 11 '15 at 14:49
  • Lexical environment = window? Is lexical environment, it is not the object variables of the function? – Aleksandr Feb 11 '15 at 15:30
  • Looks like I've messed with the [Lexical Environment](http://es5.github.io/#x10.2) and [Lexical Records](http://es5.github.io/#x10.2.1). – Teemu Feb 11 '15 at 15:35

1 Answers1

1

Namely this.method he gets there or not?

No, .method is becoming a property of the instance object. It's not becoming a variable in the lexical environment.

Right?

Yeah, your diagram looks fine.

execution context (contained in the stack)
  lexical environment       <----,
    outer: [global scope]        |
    environment: {               |
      a: 10,                     |
      f: function {              |
        [[scope]]: environment --´
        [[code]]: …
        prototype: …
      }
    }
  variable environment: (same as lexical)
  this binding: object {
    [[prototype]]: Func.prototype
    method: function {…}
  }
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thank you very much for your reply. I really like your answers! Please look at this code: http://jsfiddle.net/shhwnxsr/ I'm trying to deal with it, and I have a few questions you could not answer them? I asked questions in the comments. – Aleksandr Feb 11 '15 at 15:52
  • 1
    `var parentEnable = this.enable` is a reference to the function that Machine wrote in the `.enable` property of the created object. When it is called, it's `[[scope]]` does still point to its - non-garbagecollected - parent lexical environment (the one created when calling `Machine`), and that is used in the scope chain (`outer`) for the new execution context. See also http://stackoverflow.com/q/111102/1048572 – Bergi Feb 11 '15 at 16:01
  • Do I understand correctly that [[Scope]] parentEnable indicates the Machine? – Aleksandr Feb 11 '15 at 16:50
  • 1
    No, it references the lexical context that was created by the `Machine()` *call* that also created just this `enable` function. Its environment only contains the `self` variable, no reference to `Machine`. – Bergi Feb 11 '15 at 16:53
  • So in parentEnable [[Scope]] will also indicate the lexical environment Machine? Right? http://jsfiddle.net/shhwnxsr/4/ – Aleksandr Feb 11 '15 at 17:16
  • 1
    It's the same object, yes. – Bergi Feb 11 '15 at 17:19