我可以算一个物体吗?像:
var student1 = new Student();
student1.name("ziv");
student1.age();
student1.where("a");
var student2 = new Student();
student2.name("dani");
student2.age();
student2.where("b");
var student3 = new Student();
student3.name("bari");
student3.age();
student3.where("c");
一些将计算它们并返回3的函数。
谢谢:)
答案 0 :(得分:2)
我想你的意思是计算实例。您必须跟踪实例,例如使用数组
var students = [];
var student1 = new Student()
,student2 = new Student();
students.push(students1,students2);
/* later ... */
var nOfStudents = students.length; //=> 2
另一个想法是为Student
原型添加一个计数器:
Student.prototype.instanceCnt = 0;
并在每个实例的学生构造函数中递增它
//in the Student constructor function
function Student(){
this.instanceCnt += 1;
/* ... */
}
// usage example
var student1 = new Student()
,student2 = new Student()
,students = student1.instanceCnt; //=> 2
答案 1 :(得分:1)
不,您需要在Student
构造函数中手动添加计数器,或将每个实例附加到数组并获取该数组的长度。
F.ex:
var counter;
function Student() {
counter++; // this will increase every time Student is initialized
// continue the constructor...
}
或者:
var students = [];
function Student() {
students.push(this); // this will hold the instance in an array
// continue the constructor...
}
console.log(students.length);
答案 2 :(得分:1)
Javascript中没有静态概念,但您可以使用闭包来模拟该功能。
(function(){
var numberOfInstances = 0;
var Student = function(studentName){
var name = '';
var _init(){
name = studentName ? studentName : 'No Name Provided';
numberOfInstances++;
}();
this.getName = function(){
return name;
};
this.getNumberOfInstances = function(){
return numberOfInstances;
};
return this;
};
})();
var student1 = new Student("steve");
var student2 = new Student("sally");
console.log("my name is " + student1.getName());
console.log("my name is " + student2.getName());
console.log("number of students => " + student1.getNumberOfInstances());
console.log("number of students => " + student2.getNumberOfInstances());
答案 3 :(得分:0)
您可以编写一个简单的通用实例化/继承帮助程序来跟踪其在数组中的实例
这样的事可能会这样做
var base = (function baseConstructor() {
var obj = {
create:function instantiation() {
if(this != base) {
var instance = Object.create(this.pub);
this.init.apply(instance,arguments);
this.instances.push(instance);
return instance;
} else {
throw new Error("You can't create instances of base");
}
},
inherit:function inheritation() {
var sub = Object.create(this);
sub.pub = Object.create(this.pub);
sub.sup = this;
return sub;
},
initclosure:function initiation() {},
instances: [],
pub: {}
};
Object.defineProperty(obj,"init",{
set:function (fn) {
if (typeof fn != "function")
throw new Error("init has to be a function");
if (!this.hasOwnProperty("initclosure"))
this.initclosure = fn;
},
get:function () {
var that = this;
//console.log(that)
return function() {
if(that.pub.isPrototypeOf(this)) //!(obj.isPrototypeOf(this) || that == this))
that.initclosure.apply(this,arguments);
else
throw new Error("init can't be called directly");
};
}
});
Object.defineProperty(obj,"create",{configurable:false,writable:false});
Object.defineProperty(obj,"inherit",{configurable:false,writable:false});
return obj;
})();
var Student = base.inherit()
Student.init = function (age) {
this.age = age;
}
var student1 = Student.create(21)
var student2 = Student.create(19)
var student3 = Student.create(24)
console.log(Student.instances.length) // 3
为例