1

Recently I am learning to make javascript class, I saw other people make class like this

var item = {
     'a':{'b':1000,'c':2000,'d':3000} , //how to make a variable become an array?
     myfunction : function(){
         console.log(item.a.b); //and output item.a[1]?
     }
 };
 item.myfunction();

But is it possible to make variable a an array? and output a variable item.a[1] like that?I tried

var a=new Array('1000','2000','3000') but error, I need a correct syntax.

1
  • If you don't need 'b', 'c', or 'd', then you should be able to use what you have, or even better: 'a':[1000, 2000, 3000], Commented Nov 24, 2012 at 3:58

3 Answers 3

2
var item = {
    'a': [1000, 2000, 3000],
    myfunction: function () {
        console.log(this.a[1]);
    }
};

But that's not a class, and there is no "variable a".

Sign up to request clarification or add additional context in comments.

4 Comments

thanks,but I guess it is a class, if I use var item2 = new item(); ?
No, JavaScript has no classes (and new takes a function, so new item would be an error).
First sentence on that page: "JavaScript is a class-free, object-oriented language". new Object() works because Object is a predefined function. As for what new does exactly, see MDN.
Last sentence: "I now see my early attempts to support the classical model in JavaScript as a mistake."
1
'a':[1000,2000,3000]

console.log(item.a[0]); // 1000
console.log(item.a[2]); // 3000

Read more: http://www.elated.com/articles/javascript-array-basics/

1 Comment

so if I use this,it will be a class? function fruits(sizes,kind,colors){ this.sizes = sizes; this.kind = kind; this.colors = colors; this.mix = function(){ return this.sizes+this.kind; } } var apple = new fruits(1,1,'red');
0

You have 2 ways depending on if you want to convert "a" to an array or if you just want "a" to be an array.

The first method would be to run it through a function

function toArray(object) {
    var array = new array();
    for (key in object) {
        if (object.hasOwnProperty(key)) {
            array.push(object);  // This is alternative for indexed array
            array[key] = object; // This is for an associative array
        }
    }
    return array;
}

Maybe have your "class" call this function and assign the return value to "a"

var item = {
    'a':{'b':1000,'c':2000,'d':3000} , //how to make a variable become an array?
    myfunction : function(){
        this.a = toArray(this.a);
        console.log(this.a[1]) // If you do indexed
        console.log(this.a.b); // If you do associative
    }
};

item.myfunction();

The second way was explained by @ahren which is just to define "a" as an array by [...] the values instead of {...}

You can also check out my own question put up about OOP in JavaScript, got a nice reply from T.J. about it there concerning private methods.

JavaScript OOP Pitfalls

Don't know if this is what you wanted but should work.

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.