繁体   English   中英

在JavaScript中使用默认值制作自定义对象的数组

[英]making an array of custom objects with default values in JavaScript

所以我想做的是自定义对象的数组。 因此,我有一个构造函数,但我没有通过参数传递值,而是想将所有值初始化为0,并使用getter或setter来检索它们。 显然,它不起作用,但我不知道为什么。 我到处看的对象都是通过参数传递值来创建的,所以我只是在猜。

我的构造函数

    function Stol()
    {
        var cipsy = 0, kofca = 0, dzus = 0, sypcaj = 0, vrecaj = 0, coko = 0,jedlo = 0, pernik = 0, kava = 0, ucet = 0;
        this.cipsy = cipsy;
        this.kofca = kofca;
        this.dzus = dzus;
        this.sypcaj = sypcaj;
        this.vrecaj = vrecaj;
        this.coko = coko;
        this.jedlo = jedlo;
        this.pernik = pernik;
        this.kava = kava;
        this.ucet = ucet;

        this.reset = reset;
        function reset() {
            this.cipsy = 0;
            this.kofca = 0;
            this.dzus = 0;
            this.sypcaj = 0;
            this.vrecaj = 0;
            this.coko = 0;
            this.jedlo = 0;
            this.pernik = 0;
            this.kava = 0;
            Obrat += this.ucet;
            this.ucet = 0;
        }
        this.addItem = addItem;
        function addItem(type, number) {
            switch (type) {
                case 0 : this.cipsy += number; break;
                case 1 : this.kofca += number; break;
                case 2 : this.dzus += number; break;
                case 3 : this.sypcaj += number; break;
                case 4 : this.vrecaj += number; break;
                case 5 : this.coko += number; break;
                case 6 : this.jedlo += number; break;
                case 7 : this.pernik += number; break;
                case 8 : this.kava += number; break;
            }
        }
        this.getItem = getItem;
        function getItem(type) {
            var item;
            switch (type) {
                case 0 : item = this.cipsy; break;
                case 1 : item = this.kofca; break;
                case 2 : item = this.dzus; break;
                case 3 : item = this.sypcaj; break;
                case 4 : item = this.vrecaj; break;
                case 5 : item = this.coko; break;
                case 6 : item = this.jedlo; break;
                case 7 : item = this.pernik; break;
                case 8 : item = this.kava; break;
            }
            return item;
        }
    }

然后在这里我创建数组

var stol = new Array();
for (var i = 0; i < 14; i++) {
    stol[i] = new Stol();
}

最终我想像这样使用jQuery修改一些范围。 #selecStol是一个下拉列表(切换尚未完成)。

$("#selecStol").change(function(){
            var myStol = stol[$(this).find(":selected").val()];
            for (i = 0; i < 14; i++) {
                switch (i) {
                    case 0 : $("#cipsy").text(myStol.getItem(i));break;

                }
            }
        })

但这不起作用,我也不知道哪一部分。

这是因为this在您的getter函数是不是你的想法。

如果在开发工具中对此进行检查,则可能会看到window对象。 为了解决这个问题,您可以将this保存在this的var中:

function Stol(){
    var self = this;
    //All your variable "this"
    this.getItem = function(type){
        //In this function you use self instead of this
        //ex : case 0 : item = self.cipsy; break;
    }
}

还是建议的方式,使用prototype:

function Stol(){
     //Your things
}

Stol.prototype.getItem = function(type){
    //Now "this" will be Stol object.
}

请注意,我仅将getItem函数用于答案,但是所有其他函数都存在相同的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM