1

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
1
  • @Arslan Ahson I think you should read a little and then come here to ask questions that are explained even in Wikipedia! Commented Sep 19, 2011 at 13:10

5 Answers 5

1
function abc(){
 this.a = 1;
 this.b = 2;
}
var obj = new abc();
obj.list = ["abc"];
answered Sep 19, 2011 at 13:07
Sign up to request clarification or add additional context in comments.

Comments

1
var obj = new abc();
obj.list = [];
obj.list.unshift("abc");
answered Sep 19, 2011 at 13:05

Comments

0
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

Comments

0

I think you need to write

obj.push("abc")
answered Sep 19, 2011 at 13:06

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.