Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以使用字符串作为路径?
代替_parent._parent.myVar=123;
_parent._parent.myVar=123;
使用这样的东西:
var path = '_parent._parent';
然后将其用作
path.myVar=123';
您可以使用多个[].
[]
对于您的示例,您需要_parent._parent:
_parent._parent
var path = '_parent'; this[path][path].myVar = 123;
另一个例子:
var path1 = '_parent'; var path2 = 'apple'; this[path1][path2].myVar = 456; //equals to _parent.apple.myVar = 456;