JS-Problem beim Deklarieren eines Arrays in der Klasse
淡淡烟草味
淡淡烟草味 2017-05-19 10:25:21
0
2
667
var dynamic_particle=function(){
        this.init();
    }
dynamic_particle.prototype={
       init:function(){
           this.a=new Array();
           this.a.push(1);
           alert(a[0]);
        }
    }

aDer Fehler ist undefiniert

淡淡烟草味
淡淡烟草味

Antworte allen(2)
仅有的幸福
var dynamic_particle=function(){
        this.init();
    }
dynamic_particle.prototype={
       init:function(){
           this.a=new Array();
           this.a.push(1);
           alert(this.a[0]);
        }
    }

你都知道前面用this.a ,alert的时候为何不加上this.呢……肯定报错

習慣沉默

封装方式,一般是这样使用的:

var defOpt = {
    a: [1]
}

var dynamic_particle=function(options){
    if(!(this instanceof dynamic_particle)){
        return new dynamic_particle(options);
    }
    this.opt = $.extend({}, defOpt, options);
    this.init();
}
dynamic_particle.prototype={

    init:function(){
       console.log('初始化数据:', this.opt);
    },
    other: function() {
        console.log('调用option中的新数据', this.opt.newData);
    }
}
var dp = dynamic_particle({
    newData: [2] 
})
dp.other()
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage