How to get property values ​​from one object in javascript?

If I have this in javascript

var x = {
    "a" : function(q) { return q+1; },
    "b" : function(w) { return (this["a"])(1); } 
};

I want to do something, for example, access functions from the same object, but different properties. For example, in the above code, the property amust be exact, but in the property bI want to use the function stored in a. For the time being, I'm just this["a"], which probably doesn't work. But is there any way to get this feature?

thank

+4
source share
3 answers

What do you actually work. Check out this script here with your object ..

Javascript

var x = {
    "a" : function(q) { return q+1; },
    "b" : function(w) { return (this["a"])(1); } 
};

document.body.innerHTML = x.b();

The result of the call x.b()is 2.

:

var myObj = {
    myFunction: function(testVar)
    {
        return this.Utilities.helperFunction(testVar);
    },
    Utilities: {
        helperFunction: function(anotherVar)
        {
            return anotherVar * 2;
        }
    }
}

, . this["a"], this.a, . , this["a"], - in, .

this

+4

:

var x = {
        "a" : function(q) {
            return q+1;
        },

        "b" : function(w) {
            return (this.a)(1);
        }
    };

    document.write(x.b(1));
0

. : "his" ?

; this - , x.b=function() {this //}, this 1 x.

0

Source: https://habr.com/ru/post/1531990/


All Articles