简体   繁体   English

从子类访问父类

[英]Access parent class from child class

    var config = {
        a: 'test',
        b: {
            c: // SOmeway to access config a value
        }
    }

IS there any way to access value from class b? 有什么办法可以从B类获取价值?

Neither of those things is a class. 这些东西都不是一类。 They're objects. 他们是对象。

No, there's no way to access config.a where you have c: in your code, all within the same object literal. 不,无法访问config.a ,在代码中的c:中,所有对象都在同一对象常量内。 You can do it after the fact: 您可以在事实发生后执行以下操作:

var config = {
    a: 'test',
    b: {
    }
};
config.b.c = config.a;

Note that config.bc will receive the value of config.a . 请注意, config.bc将接收config.a If someone updates config.a later, config.bc will not be updated. 如果以后有人更新config.a ,则不会更新config.bc

If c were a function , you could access config.a from within it: 如果c是一个函数 ,则可以从中访问config.a

var config = {
    a: 'test',
    b: {
        c: function() {
           // You can use `config.a` here
        }
    }
};

There, each time you call c , you'll be using the current value of config.a . 在那里,每次调用c ,都将使用config.a当前值。

Try this i found it in search in for javascript 试试这个我在搜索JavaScript中找到它

this.base or this.base() this.base或this.base()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM