Accessing variable from JavaScript object

MaxEcho

I have following script

<script>

var Obj = {
        purpose_1: {
            defaults: { 
                purpose:{ my_variable:"Test" } 
            },
            activity: function() {
                console.log( Obj.purpose_1.defaults.purpose.my_variable );
            }
        },

        purpose_2: {

        }
   }

   Obj.purpose_1.activity();

</script>

I am accessing my_variable and getting output Test in console from line

console.log( Obj.purpose_1.defaults.purpose.my_variable );

Is there any short cut way like

this.my_variable

to access my_variable instead of this long path

Obj.purpose_1.defaults.purpose.my_variable

Thanks.

Niccolò Campolungo
activity: function () {
    return function() {
        //this now refers to Obj.purpose_1.defaults.purpose
        console.log(this.my_variable);
    }.call(Obj.purpose_1.defaults.purpose);
    // call overrides the context in which the function is executed
}

Return a function bound to the context you need! Here is a sample fiddle.

See the Function.prototype.call method at MDN.

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Javascript accessing class variables from window object

분류에서Dev

Accessing a variable in a void from main

분류에서Dev

Accessing AR from value object

분류에서Dev

Having trouble accessing Object element with a variable

분류에서Dev

Javascript: Accessing multi-dimensional object properties

분류에서Dev

accessing a variable from outside the function in python

분류에서Dev

Object of main activity in result activity not properly accessing boolean variable?

분류에서Dev

Accessing private instance variable of inner class from outer class

분류에서Dev

Accessing a local variable whose name comes from user input

분류에서Dev

Accessing $scope variable from another method in the same controller Angularjs

분류에서Dev

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

분류에서Dev

Accessing namespace script variable

분류에서Dev

Accessing variable of a method in superclass

분류에서Dev

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

분류에서Dev

Javascript "undefined" variable gets the matching HTML object

분류에서Dev

How to use JavaScript variable value as Object selector

분류에서Dev

javascript variable value lost when outside object

분류에서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 TimePicker value in Javascript

분류에서Dev

Accessing values in javascript

분류에서Dev

Accessing JSON object in Handlebars [object Object] error

분류에서Dev

VSTO: accessing CommandBarButton with variable yields invalid reference - Exception from HRESULT: 0x800A01A8

분류에서Dev

Accessing winreg from jython

분류에서Dev

Syntax for Accessing Dereferenced Values of an Object

분류에서Dev

Accessing Ember object instances in debugger

분류에서Dev

cytoscape.js accessing object

분류에서Dev

cytoscape.js accessing object

Related 관련 기사

  1. 1

    Javascript accessing class variables from window object

  2. 2

    Accessing a variable in a void from main

  3. 3

    Accessing AR from value object

  4. 4

    Having trouble accessing Object element with a variable

  5. 5

    Javascript: Accessing multi-dimensional object properties

  6. 6

    accessing a variable from outside the function in python

  7. 7

    Object of main activity in result activity not properly accessing boolean variable?

  8. 8

    Accessing private instance variable of inner class from outer class

  9. 9

    Accessing a local variable whose name comes from user input

  10. 10

    Accessing $scope variable from another method in the same controller Angularjs

  11. 11

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

  12. 12

    Accessing namespace script variable

  13. 13

    Accessing variable of a method in superclass

  14. 14

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

  15. 15

    Javascript "undefined" variable gets the matching HTML object

  16. 16

    How to use JavaScript variable value as Object selector

  17. 17

    javascript variable value lost when outside object

  18. 18

    Accessing object in array recreating object?

  19. 19

    Javascript - count and remove from an object

  20. 20

    Returning values from an Object in Javascript

  21. 21

    accessing TimePicker value in Javascript

  22. 22

    Accessing values in javascript

  23. 23

    Accessing JSON object in Handlebars [object Object] error

  24. 24

    VSTO: accessing CommandBarButton with variable yields invalid reference - Exception from HRESULT: 0x800A01A8

  25. 25

    Accessing winreg from jython

  26. 26

    Syntax for Accessing Dereferenced Values of an Object

  27. 27

    Accessing Ember object instances in debugger

  28. 28

    cytoscape.js accessing object

  29. 29

    cytoscape.js accessing object

뜨겁다태그

보관