javascript arraylist
Can we attach arraylist or func开发者_运维百科tion with a javascript object at runtime
function abc(){
this.a = 1;
this.b = 2;
}
var obj = new abc();
obj.list.add("abc"); // list is not declare in class abc
function abc(){
this.a = 1;
this.b = 2;
}
var obj = new abc();
obj.list = ["abc"];
var obj = new abc();
obj.list = [];
obj.list.unshift("abc");
function abc(){
this.a = 1;
this.b = 2;
}
var obj = new abc();
obj.list = new Array();
obj.list.push("abc");
I think you need to write
obj.push("abc")
you will first have to declare the list, then you could access/modify it as any other object prop :
var obj = new abc();
obj.list = [];//or obj.list = new Array();
obj.list.add("abc");
精彩评论