Javascript accessing class variables from window object

kaushal

I was wondering if it is possible to access foo from window.updateFoo() in the code below:

function f1 () {
    'use strict';
    this.x = {};
    this.x.foo = 0;

    window.updateFoo = function(val){
       this.x.foo = val;       // Obviously wrong since 'this' doesn't refer to f1 now. Uncaught TypeError: Cannot set property 'foo' of undefined 
    };
    window.updateFoo(20);      // Try changing the value of this.x.foo?
}
Scimonster

When you call window.updateFoo, it is called in the context of window. Your best option is to save this in a variable, and then use that in the function:

function f1 () {
    'use strict';
    this.x = {};                // Object for 'const' variables
    this.x.foo = 0;

    var _this = this;

    window.updateFoo = function(val){
       _this.x.foo = val;       // Use saved version
    };
    window.updateFoo(20);      // Try changing the value of this.x.foo?
}

The other option, but it will only work within f1, is to call updateFoo will a specific context:

window.updateFoo.call(this, 20);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Accessing timeline variables from a class?

분류에서Dev

Accessing variable from JavaScript object

분류에서Dev

Accessing nested JavaScript objects with variables

분류에서Dev

Comparing variables of class Object

분류에서Dev

Accessing AR from value object

분류에서Dev

Accessing javascript variables across html files

분류에서Dev

Ruby Beginner - Accessing Another Class Object

분류에서Dev

Accessing R's Environmental Variables from Bash

분류에서Dev

accessing variables from main method in python idle

분류에서Dev

how to create variables from key value object pair in javascript

분류에서Dev

Javascript: Accessing multi-dimensional object properties

분류에서Dev

Accessing the EntityManager from a JSF Converter class

분류에서Dev

Accessing private instance variable of inner class from outer class

분류에서Dev

Accessing derived class member from base class pointer

분류에서Dev

Accessing properties of a spritebuilder object outside of its class in Xcode?

분류에서Dev

Copy Javascript "Class" Prototype to an Object

분류에서Dev

Incorrect array values when accessing an array from another class?

분류에서Dev

Accessing files stored in Swift object from inside a OpenStack VM instance

분류에서Dev

Accessing Environment Variables in flash

분류에서Dev

Accessing Static variables in cakephp

분류에서Dev

Variables not updated from within baseHTTPserver class

분류에서Dev

Variables not updated from within baseHTTPserver class

분류에서Dev

Exclude javascript window resize from mobile devices

분류에서Dev

Accessing the Properties of a class dynamically

분류에서Dev

Why do you need to attach or reference and object when accessing another class variable in unity?

분류에서Dev

Accessing object in array recreating object?

분류에서Dev

Javascript - count and remove from an object

분류에서Dev

Returning values from an Object in Javascript

분류에서Dev

Accessing nested hashes using variables

Related 관련 기사

  1. 1

    Accessing timeline variables from a class?

  2. 2

    Accessing variable from JavaScript object

  3. 3

    Accessing nested JavaScript objects with variables

  4. 4

    Comparing variables of class Object

  5. 5

    Accessing AR from value object

  6. 6

    Accessing javascript variables across html files

  7. 7

    Ruby Beginner - Accessing Another Class Object

  8. 8

    Accessing R's Environmental Variables from Bash

  9. 9

    accessing variables from main method in python idle

  10. 10

    how to create variables from key value object pair in javascript

  11. 11

    Javascript: Accessing multi-dimensional object properties

  12. 12

    Accessing the EntityManager from a JSF Converter class

  13. 13

    Accessing private instance variable of inner class from outer class

  14. 14

    Accessing derived class member from base class pointer

  15. 15

    Accessing properties of a spritebuilder object outside of its class in Xcode?

  16. 16

    Copy Javascript "Class" Prototype to an Object

  17. 17

    Incorrect array values when accessing an array from another class?

  18. 18

    Accessing files stored in Swift object from inside a OpenStack VM instance

  19. 19

    Accessing Environment Variables in flash

  20. 20

    Accessing Static variables in cakephp

  21. 21

    Variables not updated from within baseHTTPserver class

  22. 22

    Variables not updated from within baseHTTPserver class

  23. 23

    Exclude javascript window resize from mobile devices

  24. 24

    Accessing the Properties of a class dynamically

  25. 25

    Why do you need to attach or reference and object when accessing another class variable in unity?

  26. 26

    Accessing object in array recreating object?

  27. 27

    Javascript - count and remove from an object

  28. 28

    Returning values from an Object in Javascript

  29. 29

    Accessing nested hashes using variables

뜨겁다태그

보관