How does `this` work in default parameters?

夙愿已清 提交于 2019-11-30 03:54:22

My question is, how does this play into this? I don't get it. Are they are really evaluated at call time?

Yes, the parameter initializers are evaluated at call time. It's complicated, but the steps are basically as follows:

  1. A new execution context is established on the stack,
    with a new environment in the "closure scope" of the called function
  2. If necessary, it's thisBinding is initialised
  3. Declarations are instantiated:
    1. Mutable bindings for the parameter names are created
    2. If necessary, an arguments object is created an bound
    3. The bindings are iteratively initialised from the arguments list (including all destructurings etc)
      In the course of this, initialisers are evaluated
    4. If any closures were involved, a new environment is inserted
    5. Mutable bindings for the variables declared in the function body are created (if not already done by parameter names) and initialised with undefined
    6. Bindings for let and const variables in the function body are created
    7. The bindings for functions (from function declarations in the body) are initialised with instantiated functions
  4. Finally the body of the function is evaluated.

So parameter initialisers do have access to the this and the arguments of the call, to previously initialised other parameters, and everything that is in their "upper" lexical scope. They are not affected by the variables declared in the function body (though they are affected by all the other parameters, even if in their temporal dead zone).

what about this:

function bar (thing = x) {}
{
  let x = 'x from foo';
  return bar();
}

I don't get it. Why does bar not take the x from foo when called inside foo?

Because x is a local variable that bar does not have access to. We're so lucky that they are not dynamically scoped! The parameter initialisers are not evaluated at the call site, but inside the called function's scope. In this case, the x identifier is resolved to the global x variable.

When they say "evaluated at call time", I think they're referring to a call-by-name expression. Here's how babel outputs your third example:

'use strict';

var x = 'x from global';

function bar() {
  var thing = arguments[0] === undefined ? x : arguments[0];

  return thing;
}

function foo() {
  var x = 'x from foo';
  return bar();
}

bar() === foo(); // what?

Since var x is inherited within the lexical scope of bar from the global scope, that is the scope in which it is used.

Now, consider the following:

let i = 0;

function id() {
  return i++;
}

function bar (thing = id()) {
  return thing;
}

console.info(bar() === bar()); // false

Which transpiles to

"use strict";

var i = 0;

function id() {
  return i++;
}

function bar() {
  var thing = arguments[0] === undefined ? id() : arguments[0];

  return thing;
}

console.info(bar() === bar()); // false

Notice how here, id is called inside the function, rather than being cached and memoized at the time the function is defined, hence the call-by-name rather than call-by-value.

So the behavior is actually correct in your second example. There is no y.foo and since this is dynamically scoped in Javascript (i.e. it varies based on the receiver of a given function invocation), when y.z() looks for this.foo, it will look for it in y, so y.z() will return undefined, while x.foo() will just return the foo function itself.

If you do want to bind to the receiver you can bind foo to x when you assign it. Then it should work as expected.

Sorry if any of this is unclear; let me know in the comments and I'd be happy to clarify! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!