(我不敢相信这是不可能的,但我可能没有使用正确的搜索词)
我有一个这样的“类”:
function MyClass() = {
this.getItem = function(key) {
// return whatever;
};
this.setItem = function(key, val) {
// set whatever
};
};
是否可以通过(我认为所谓的)索引属性获取/设置?
var test = new MyClass();
test["key"] = "value";
alert(test["key"]);
[ ] 功能)
最佳答案
在 JavaScript 中,您没有那种方式的自动访问器。至少我没见过。
你可以使用
var test = new MyClass();
test.key = "value";
alert(test.key);
和
alert(test["key"]);
但这可能不是您想要的...
关于javascript - javascript中的索引属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5310529/