Hi Charles, I think this discussion is not really appropriate for this ML
but I'd like to point out you have already all possible solutions.
For instance, you don't need to bond at all for your case since you use
`.call` already. Please be sure you understand what `.call` does because
it's kinda pointless to use both `.call` and `.bind` or bound variables.
Example:
```js
function outer() {
function inner() {
console.log(this.a);
}
inner.call({});
}
outer.call({a: 1});
```
If you need to bring in the default `this` arrow function does exactly that
and it will be pointless to `.call` an arrow function with a different
context.
```js
function outer() {
const inner = () => {
console.log(this.a);
};
inner.call({}); // or just inner();
}
outer.call({a: 1});
```
If you'd like to create an inner function with a different context you can
use `.bind` which has been in core for quite a while and its purpose is
exactly that one.
```js
function outer() {
const inner = function () {
console.log(this.a);
}.bind({});
inner();
}
outer.call({a: 1});
```
As you can see the last thing we need is yet another way to bind something
in the language ... but of course, if you really want to use the `var self
= this` pattern you are free to do that, no need to change specifications
though.
Eventually, what you might want is a hsortcut for function like `->`
instead of `=>` could be ... but that's a whole different story.
Best Regards
On Fri, Jan 29, 2016 at 3:14 PM, ` Mystery . <[email protected]> wrote:
> Hi,
>
> Thanks for the replies. I'm aware of the arrow functions, but the reason I
> don't use them here is that I wish to bind a custom context to the inner
> function (i.e. preserve the "this" passed while calling the inner function,
> like a custom context for promise callbacks for example) and use some
> variables in the outer function's context as well (might be a class
> method). Guess I should fallback to old "saving this to a variable"
> technique in this situation.
>
> IMHO I don't think the default parameters should be evaluated within the
> context of the function being called, at least not while it's outside of
> function body's region...... Is there a specific reason to do that, or it's
> just a design preference?
>
> Yours,
> Charles Weng
>
> On Fri, Jan 29, 2016 at 9:27 PM Bergi <[email protected]> wrote:
>
>> ` Mystery . wrote:
>> > During the development of an ES6 web application, I came across a
>> situation
>> > where I wanted to bind 'this' of the outer function to a parameter of an
>> > inner function
>>
>> Use an arrow function for that:
>> ```
>> function outer() {
>> const inner = (_this = this) => {
>> console.log(_this.a); // 1
>> }
>> inner.call({});
>> }
>> outer.call({a: 1});
>>
>> > In the latest Firefox, the above script prints 'undefined'. This seems
>> > pretty counter-intuitive, and it also fails when I try to pass 'this.a'
>> as
>> > default value for the parameter of the inner function. I wonder if this
>> is
>> > an intended behavior? Thanks in advance.
>>
>> Yes, it is intended, and pretty intuitive - default initialisers are
>> evaluated in the scope of the called function, at the call - they're not
>> values that are computed at the time of the definition of the function.
>>
>> Regards,
>> Bergi
>> _______________________________________________
>> es-discuss mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss