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 
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"];
Sign up to request clarification or add additional context in comments.

Comments

1
var obj = new abc();
obj.list = [];
obj.list.unshift("abc");

Comments

0
function abc(){
        this.a = 1;
        this.b = 2;
    }
    var obj = new abc();
    obj.list = new Array();
    obj.list.push("abc");

Comments

0

I think you need to write

obj.push("abc")

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");

Comments

Your Answer

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.