如何在JavaScript类中创建数组? [英] How to make array at javascript class?

查看:92
本文介绍了如何在JavaScript类中创建数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在学习制作javascript类,我看到其他人都是这样做的类。

  var item = {
'a':{'b':1000,'c':2000,'d':3000},//如何让变量变成数组?
myfunction:function(){
console.log(item.a.b); //并输出item.a [1]?
}
};
item.myfunction();

但是可以使变量 a 数组?并输出一个变量 item.a [1] 就像那样?我试过了

var a = new Array('1000','2000','3000')
但是错误,我需要一个正确的语法。

解决方案

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

但这不是一个类,也没有变量a。


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.

解决方案

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".

这篇关于如何在JavaScript类中创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆