EDIT:
========================================================
Please see revision 13 of this test for answers:
http://jsperf.com/closure-prototype-static-performance/
========================================================
我通常更喜欢这样声明我的类,因为我不是对象字面量方式的忠实粉丝:
第一种方式:私有范围
function Employees() {
var persons = []; // [Person, Person]
this.isEmployed = function(_firstName, _lastName) {
for ( var i = 0; i < persons.length ; i++ ) {
if ( persons[i].equals(_firstName, _lastName) ) {
return true;
}
}
return false;
}
}
function Person(_firstName, _lastName) {
this.equals = function(firstName, lastName) {
return _firstName == firstName && _lastName == lastName;
}
}
第二种方式:原型
另一种方法是使用原型,但是您将无法访问“私有”属性,例如 var people = [] 和 Person(...) 中的参数,但是人们可以忍受将它们公开,尽管对于 person将引入两个新属性:
function Employees() {
this.persons = []; // [Person, Person]
}
Employees.prototype.isEmployed = function(_firstName, _lastName) {
for ( var i = 0; i < this.persons.length ; i++ ) {
if ( this.persons[i].equals(_firstName, _lastName) ) {
return true;
}
}
return false;
};
function Person(_firstName, _lastName) {
this.firstName = _firstName; // Two new properties. Are these or the function declared more expensive?
this.lastName = _lastName;
}
Person.prototype.equals = function(firstName, lastName) {
return this.firstName == firstName && this.lastName == lastName;
};
第三种方法:静态方法
另一种选择是像原型一样做,而是在“静态”之外创建方法:
function Employees() {
this.persons = []; // [Person, Person]
}
function isEmployed(employee, _firstName, _lastName) {
for ( var i = 0; i < employee.persons.length ; i++ ) {
if ( equals(employee.persons[i], _firstName, _lastName) ) {
return true;
}
}
return false;
}
function Person(_firstName, _lastName) {
this.firstName = _firstName; // Two new properties. Are these or the function declared more expensive?
this.lastName = _lastName;
}
function equals(person, firstName, lastName) {
return person.firstName == firstName && person.lastName == lastName;
}
问题:
1. 您认为哪种方法最适合性能、内存和 CPU 功率,是否有任何理由说明为什么或何时应该尝试避免或遵循另一种?
2. 从这个角度来看,最后一个具有“静态”作用域的示例是否比原型方法更好?
3. 当谈到第一种方式时,这是我更喜欢的方式,因为它更整洁,允许“私有”范围,并且所有代码都很好地适合,编译器可能会为你做原型方法,或者是这些函数一遍又一遍地为每个新的 Person 声明你做什么?
当我执行 new() 并且涉及很多方法时,我尽量避免使用第一种方法,然后我通常会采用静态方法,尽管我想知道我是否有任何真正好的理由这样做?
4. 我注意到我的示例没有包含“私有”函数,那么编译器如何处理它们?
例子:
function Person(_firstName, _lastName) {
function privateFunction() {
// How does the compile treat private functions. Are they also created once per new Person() ? Cheap or expensive?
}
}