简体   繁体   English

性能:私有方法原型

[英]Performance: prototype private methods

Given the object: 给定对象:

object = function ()
{
   var pub = {
      m1: function () {
        m2();
        m3();
      }
   };

   function m2() {
     ...
   }

   function m3() {
     ...
   }

   return pub;
}

This object will be instantiate more than one time. 该对象将实例化一次以上。 Should I add the private functions to the prototype, so they are not copied in every instance? 我是否应该将私有函数添加到原型中,以便不在每个实例中都复制它们?

What could be a limit, perhaps, to a number X of private methods vs number Y of instances. 私有方法的数量X实例的数量Y可能是一个限制。

In OOP Javascript with ES5, your objects can have 3 types of member. 在带有ES5的OOP Javascript中,您的对象可以具有3种类型的成员。

  • public 上市
  • private 私人的
  • priviledged 特权

public members are created in the prototype and can be accessed by anyone. 公共成员是在原型中创建的,任何人都可以访问。

var MyCustomClass = function() {
};

MyCustomClass.prototype.myPublicMethod = function() {
};

var myCustomObject = new MyCustomClass();
myCustomObject.myPublicMethod();

The private members are created inside the Class constructors and can only be accessed inside the class constructor (you can't access them in the public methods, despite being in the same Class). 私有成员是在Class构造函数内部创建的,并且只能在class构造函数内部进行访问(尽管位于同一Class中,但是您不能在公共方法中访问它们)。

var MyCustomClass = function() {
    var m_privateMember; //cannot be accessed outside the constructor
};

The privileged members are actually public members, but declared inside the constructor, so they can use private members. 特权成员实际上是公共成员,但在构造函数内部声明,因此可以使用私有成员。

var MyCustomClass = function() {
    var m_privateMember = 2;

    //Priviledged method (which is a GETTER here)
    this.GetPrivateMember = function() {
        return m_privateMember;
    };
};

var myCustomObject = new MyCustomClass();
console.log(myCustomObject.GetPrivateMember()); //2

That allows you to access the private member in your public methods by calling the priviledged GETTER. 这样,您可以通过调用特权的GETTER访问公共方法中的私有成员。

Now, about performance. 现在,关于性能。 I am currently using this pattern in a project with over 700 objects with, inside them, more than 12 privates members and a lot more priviledged members (~30) because i don't like using public members (have to define members in 2 different places, the constructor and .prototype, so i just define them all in the constructor, and call the 'initialize' methods at the end of the constructor). 我目前在一个项目中使用这种模式,该项目中有700多个对象,其中包含12个以上的私有成员和更多的特权成员(约30个),因为我不喜欢使用公共成员(必须以2种不同的方式定义成员位置,构造函数和.prototype,因此我只需在构造函数中全部定义它们,然后在构造函数的末尾调用“ initialize”方法)。 And I didn't notice any performance issue. 而且我没有发现任何性能问题。 Objects are still being instantiated as fast as it can get. 对象仍在以最快的速度实例化。 I can't guarranty that there is no performance cost, but IMHO, it is not noticeable. 我不能保证没有性能成本,但是恕我直言,这并不明显。

You can also define properties this way : 您还可以通过以下方式定义属性:

var MyCustomClass = function() {
    var myPrivateMember;

    Object.defineProperty(MyCustomClass.prototype, 'myProperty', {
        get: function() {
            return myPrivateMember; 
        },
        set: function(_param) {
             myPrivateMember = _param;
        }
    });
}

var myObject= new MyCustomClass();
myObject.myProperty = 6;
myObject.myProperty; //6

But I don't like it since i prefer calling GetMyProperty(), but its a matter of taste. 但是我不喜欢它,因为我更喜欢调用GetMyProperty(),但这是一个品味问题。

You should check Object.definyProperty since i might not have the exact syntax. 您应该检查Object.definyProperty,因为我可能没有确切的语法。


EDIT for the performance issue. 编辑性能问题。

So I have run some tests in my browser (Chrome 46) and there is no performance hit between private members and public members. 因此,我已经在浏览器(Chrome 46)中运行了一些测试,并且私有成员和公共成员之间的性能均未受到影响。

For a ClassA with 12 private members vs a ClassB with 12 public members, I have instantiated 1,000,000 objects of each Class, taking on an average of 10 tests about 250 ms for ClassA and 245 ms for ClassB. 对于具有12个私有成员的ClassA与具有12个公共成员的ClassB,我实例化了每个Class的1,000,000个对象,对ClassA平均进行了10次测试,分别约为250 ms和ClassB 245 ms。

So, feel free to use private members. 因此,随时使用私人成员。

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

相关问题 私有子方法的原型 - Prototype for private sub-methods 私有和特权方法与原型方法 - Private and privileged methods vs prototype methods 具有私有方法的JS原型类不访问属性 - JS prototype class with private methods not accessing properties 使用闭包使方法在原型中私有 - Use closure to make methods private in prototype 在构造函数中创建的方法与在原型上创建的方法相比,是否存在性能损失? - Is there a performance penalty associated with methods created in the constructor versus methods created on the prototype? 可以共享范围并访问实例的私有原型方法 - Private prototype methods that can share scope and access the instance 使私有实例变量可以被匿名函数中包含的原型方法访问 - Making private instance variable accessible to prototype methods enclosed in anonymous function 共享原型方法的实例,用于私有实例变量 - Instances sharing prototype methods for use with private instance variables 通过原型定义方法与在构造函数中使用 this - 真的有性能差异吗? - Defining methods via prototype vs using this in the constructor - really a performance difference? 有没有一种方法可以利用在JavaScript工厂函数中使用原型方法的性能优势? - Is there a way to exploit the performance advantages of using prototype methods in JavaScript factory functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM