Can we attach arraylist or function 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
asked Sep 19, 2011 at 13:03
Arslan Ahson
2561 gold badge3 silver badges10 bronze badges
-
@Arslan Ahson I think you should read a little and then come here to ask questions that are explained even in Wikipedia!Bakudan– Bakudan2011年09月19日 13:10:02 +00:00Commented Sep 19, 2011 at 13:10
5 Answers 5
function abc(){
this.a = 1;
this.b = 2;
}
var obj = new abc();
obj.list = ["abc"];
answered Sep 19, 2011 at 13:07
Joseph Silber
221k59 gold badges369 silver badges293 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
var obj = new abc();
obj.list = [];
obj.list.unshift("abc");
answered Sep 19, 2011 at 13:05
Mike Thomsen
37.7k11 gold badges64 silver badges86 bronze badges
Comments
function abc(){
this.a = 1;
this.b = 2;
}
var obj = new abc();
obj.list = new Array();
obj.list.push("abc");
answered Sep 19, 2011 at 13:05
Headshota
21.4k13 gold badges63 silver badges82 bronze badges
Comments
I think you need to write
obj.push("abc")
answered Sep 19, 2011 at 13:06
Gregory Nozik
3,3823 gold badges35 silver badges48 bronze badges
Comments
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");
answered Sep 19, 2011 at 13:08
gion_13
41.5k10 gold badges99 silver badges111 bronze badges
Comments
lang-js