1

In Javascript, if I want to construct a object which can be used into polymorphism and inheritance, I would like to code like this:

function clicked(label,name,value){
    this.label=label;
    this.name=name;
    this.value=value;
}

When I want to create new object, I can use following methods:

var clicked1= new clicked("a","b","c");
var clicked2= new clicked("d","e","f");
clicked1.label="h";

But here I want to change some parameter into array. How can I do. for example, Change every label keys into label[ ], thus I can use later:

clicked1.label.push("xxx");
3
  • just.... some parameter are type of array instead of just string, I want to know how to change my code for that... Commented Mar 6, 2014 at 21:40
  • So just use an array instead of a string. What did you try writing? What don't you understand? Commented Mar 6, 2014 at 21:48
  • oh my god = =! Can I just use like: var clicked1= new clicked([],[],[]); ? I think I didn't understand this.... Commented Mar 6, 2014 at 22:00

2 Answers 2

1

Types are very simple in Javascript:

var clickedArray = new clicked([], "b", "c"); 
// label is an array in this instance of your object
clicked1.label.push("xxx");
Sign up to request clarification or add additional context in comments.

Comments

0

You can use getters/setters:

function clicked(lbl,name,value){
    var label=lbl;
    this.name=name;
    this.value=value;
    this.getLabel = function(){
        return label;
    };

    this.setLabel = function(lbl){
        label = lbl;
    };
}

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.