why doesn't 'this' of an arrow function change inside an nested object literal?

jt-wang

I found that 'this' keyword seems always point to global in using arrow function inside a nested object literal.

According to other questions, the following snippet can be explained as an arrow function's 'this' is defined in lexical context.

var c = 100;
var a = {c:5 , fn: () => {return this.c;} };
console.log(a.c); //100

However, I cannot understand the following code (nested object literal):

var c = 100;

var a = {
    c: 5,
    b: {
        c: 10,
        fn: ()=> {return this.c;}
    }
}

console.log(a.b.fn());// still 100, why not 5?

I mean, if consider from the lexical context aspect, shouldn't the 'this' in a.b.fn point to a?

Why, no matter how many levels the object is nested, do all of the 'this' instances point to window or global?

Johannes H.

The only expression in JavaScript that changes scope is a function and, as of ES6, blocks (note that an object literal is not a block, despite having curly braces around it). This means: everything that is not inside a function is in global scope.

In global scope, this refers to the global object (window in case of browsers). The only thing that changes scope is the arrow function (yes, they DO change scope!) - but it binds this lexically (which means, it uses the this from the outer scope), so it's still the global object.

If you want this to refer to the aobject, use an IIFE instead of an object literal:

var c = 100;

var a = new function () {
    this.c = 5;
    this.b = {
        c: 10,
        fn: ()=> {return this.c;}
    }
}()

alert(a.b.fn()) // 5;

Or, to bind bto this:

var c = 100;

var a = {
    c : 5,
    b : new function () {
        this.c = 10;
        this.fn = ()=> {return this.c;}
    }()
}

alert(a.b.fn()) // 10;

Alternatively, to bind this to b, you can also use a regular function instead of the arrow function:

var c = 100;

var a = {
    c: 5,
    b: {
        c: 10,
        fn: function () {return this.c;}
    }
}

alert(a.b.fn()) // 10;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

why doesn't 'this' of an arrow function change inside an nested object literal?

From Dev

Arrow Function in Object Literal

From Dev

this keyword inside nested function of object literal

From Dev

Function inside of object doesn't want to change by reference

From Dev

Why doesn't the bind() work for a function inside another function which is inside the object?

From Dev

Can't use jQuery function inside Object Literal

From Dev

Overriding function inside a object literal

From Java

Why doesn't Go allow nested function declarations (functions inside functions)?

From Java

Why doesn't this arrow function work in IE 11?

From Dev

Why "this" is undefined inside a fat arrow function definition?

From Dev

Why a value of a variable doesn't change inside backticks?

From Dev

Jquery prop function doesn't work inside Change event handler

From Dev

Why does 'this' return 'undefined' when referred in an arrow function but doesn't when called on an anonymous function?

From Dev

Data object inside the event function doesn't work and resulted to undefined

From Dev

Why I can't read the change of global variables in a nested function?

From Dev

Why late static binding doesn't work inside anonymous function?

From Dev

Why doesn't 'return' stop the function completely inside an 'if' statement?

From Dev

Why php function doesn't work inside second <?php ?>

From Dev

Why doesn't use the variable value inside the function in python

From Dev

Why php function doesn't work inside second <?php ?>

From Dev

why can't I change the variables using kwargs inside a function?

From Dev

why can't I change the variables using kwargs inside a function?

From Dev

why I can't create my array object inside function?

From Dev

In function call, why doesn't nullptr match a pointer to a template object?

From Dev

Why doesn't this object get set to NULL after the function call?

From Dev

this scope inside object literal

From Dev

How to access this.var inside an object literal function

From Dev

this is undefined inside arrow function

From Dev

Why String.Prototype replace doesn't work inside nested functions?

Related Related

  1. 1

    why doesn't 'this' of an arrow function change inside an nested object literal?

  2. 2

    Arrow Function in Object Literal

  3. 3

    this keyword inside nested function of object literal

  4. 4

    Function inside of object doesn't want to change by reference

  5. 5

    Why doesn't the bind() work for a function inside another function which is inside the object?

  6. 6

    Can't use jQuery function inside Object Literal

  7. 7

    Overriding function inside a object literal

  8. 8

    Why doesn't Go allow nested function declarations (functions inside functions)?

  9. 9

    Why doesn't this arrow function work in IE 11?

  10. 10

    Why "this" is undefined inside a fat arrow function definition?

  11. 11

    Why a value of a variable doesn't change inside backticks?

  12. 12

    Jquery prop function doesn't work inside Change event handler

  13. 13

    Why does 'this' return 'undefined' when referred in an arrow function but doesn't when called on an anonymous function?

  14. 14

    Data object inside the event function doesn't work and resulted to undefined

  15. 15

    Why I can't read the change of global variables in a nested function?

  16. 16

    Why late static binding doesn't work inside anonymous function?

  17. 17

    Why doesn't 'return' stop the function completely inside an 'if' statement?

  18. 18

    Why php function doesn't work inside second <?php ?>

  19. 19

    Why doesn't use the variable value inside the function in python

  20. 20

    Why php function doesn't work inside second <?php ?>

  21. 21

    why can't I change the variables using kwargs inside a function?

  22. 22

    why can't I change the variables using kwargs inside a function?

  23. 23

    why I can't create my array object inside function?

  24. 24

    In function call, why doesn't nullptr match a pointer to a template object?

  25. 25

    Why doesn't this object get set to NULL after the function call?

  26. 26

    this scope inside object literal

  27. 27

    How to access this.var inside an object literal function

  28. 28

    this is undefined inside arrow function

  29. 29

    Why String.Prototype replace doesn't work inside nested functions?

HotTag

Archive