请在JavaScript考虑下面的代码:如何为一个数组作为在javascript对象的属性创建方法?
function Selector() {
this.Status = "";
this.Groups = new Array();
this.Errors = new Array();
}
我想补充的方法为选择类组属性,并使用它的任何实例。我怎样才能做到这一点?
请注意,我写这篇文章的代码:
function Selector() {
this.Status = "";
this.Groups = [];
this.Groups.myFunction = function(){alert(this.length);
};
this.Errors = [];
}
var selector = new Selector();
selector.Groups = [1,2,3];
selector.Groups.myFunction();
但是,当我设置组属性,收到错误调用方法:
错误:selector.Groups.myFunction不是一个函数
我宁愿找使用原型对象的方式。
谢谢。
'this.Groups.myFunction =函数(){};' – 2012-08-10 15:59:51