JavaScript中的静态/共享属性和方法
我试图在JS中创建一个“类”,它跟踪自身实例的实例化数量。我正试图这样做JavaScript中的静态/共享属性和方法,javascript,class,static-methods,Javascript,Class,Static Methods,我试图在JS中创建一个“类”,它跟踪自身实例的实例化数量。我正试图这样做 var myNamespace = {}; myNamespace.myClass = function () { //fails here as .getNetInstanceNo() not recognised... var instanceNumber = myNamespace.myClass.getNextInstanceNo(); return { instance
var myNamespace = {};
myNamespace.myClass = function () {
//fails here as .getNetInstanceNo() not recognised...
var instanceNumber = myNamespace.myClass.getNextInstanceNo();
return {
instanceNo : function() { return instanceNumber; }
}
};
myNamespace.myClass.InstanceNo = 0; //static property?
//should the class itself have this method added to it...
myNamespace.myClass.prototype.getNextInstanceNo = function () { //static method?
return myNamespace.myClass.InstanceNo++;
};
var class1 = new myNamespace.myClass();
alert('class 1 has instance of ' + class1.instanceNo() );
但是,由于无法识别getNextInstanceNo
功能,因此此操作失败。尽管我认为我是通过myClass.prototype
添加它的
我做错了什么?
prototype
是一个对象,其他对象从中继承属性,例如,当您创建一个对象的实例而该对象没有属性/方法时,调用时,将搜索该对象所属类的原型以查找该属性/方法,下面是一个简单的示例:
function Animal(){};
Animal.prototype.Breathe = true;
var kitty= new Animal();
kitty.Breathe; // true (the prototype of kitty breathes)
var deadCat = new Animal();
deadCat.Breathe = false;
deadCat.Breathe; // false (the deadCat itself doesn't breath, even though the prototype does have breath
正如您自己所说,您不需要在prototype上定义getNextInstanceNo,因为这不是JavaScript上定义静态方法的方式,请将其保留在类本身上,相反,您可以在prototype上定义instanceNo
方法,如下所示:
var myNamespace = {};
myNamespace.myClass = function () {
this.instanceNumber = myNamespace.myClass.getNextInstanceNo();
};
myNamespace.myClass.prototype.instanceNo = function () {
return this.instanceNumber;
};
myNamespace.myClass.InstanceNo = 0;
myNamespace.myClass.getNextInstanceNo = function () {
return myNamespace.myClass.InstanceNo++;
};
var class1 = new myNamespace.myClass();
alert('class 1 has instance of ' + class1.instanceNo());
prototype
是其他对象从中继承属性的对象,例如,当您创建一个对象的实例而该对象没有属性/方法时,调用时,将搜索该对象所属类的原型以查找该属性/方法,下面是一个简单的示例:
function Animal(){};
Animal.prototype.Breathe = true;
var kitty= new Animal();
kitty.Breathe; // true (the prototype of kitty breathes)
var deadCat = new Animal();
deadCat.Breathe = false;
deadCat.Breathe; // false (the deadCat itself doesn't breath, even though the prototype does have breath
正如您自己所说,您不需要在prototype上定义getNextInstanceNo,因为这不是JavaScript上定义静态方法的方式,请将其保留在类本身上,相反,您可以在prototype上定义instanceNo
方法,如下所示:
var myNamespace = {};
myNamespace.myClass = function () {
this.instanceNumber = myNamespace.myClass.getNextInstanceNo();
};
myNamespace.myClass.prototype.instanceNo = function () {
return this.instanceNumber;
};
myNamespace.myClass.InstanceNo = 0;
myNamespace.myClass.getNextInstanceNo = function () {
return myNamespace.myClass.InstanceNo++;
};
var class1 = new myNamespace.myClass();
alert('class 1 has instance of ' + class1.instanceNo());
在中,我们可以通过简单地不向
.prototype
添加方法来解决这个问题-但我认为这就是prototype的用途?!在中,我们可以通过简单地不向.prototype
添加方法来解决这个问题-但我认为这就是prototype的用途?!因此,在我的帖子中,我说var instanceNumber=myNamespace.myClass.getNextInstanceNo()
-它实际上试图做什么,但失败了?为什么?在我的帖子中,我说var instanceNumber=myNamespace.myClass.getNextInstanceNo()
-它实际上试图做什么,但失败了?为什么?