3 回答

TA贡献1818条经验 获得超3个赞
简单的答案是JavaScript允许通过方括号访问Object的子级。
因此,您可以定义您的课程:
MyClass = function(){
// Set some defaults that belong to the class via dot syntax or array syntax.
this.some_property = 'my value is a string';
this['another_property'] = 'i am also a string';
this[0] = 1;
};
然后,您将可以使用任一语法访问类的任何实例上的成员。
foo = new MyClass();
foo.some_property; // Returns 'my value is a string'
foo['some_property']; // Returns 'my value is a string'
foo.another_property; // Returns 'i am also a string'
foo['another_property']; // Also returns 'i am also a string'
foo.0; // Syntax Error
foo[0]; // Returns 1
foo['0']; // Returns 1
添加回答
举报