简体   繁体   English

Javascript构造函数创建对象与常规对象

[英]Javascript constructor function creating object vs regular object

Sorry about the maybe misleading title, but I wonder why is this approach of creating objects and adding properties and methods: 抱歉,标题可能令人误解,但我想知道为什么这种创建对象以及添加属性和方法的方法:

//A)
var myObjCreatedByFunction = function() {
  this.prop1 = "a";
  this.func1 = function() {
   alert("a");
  }
};

and then using prototype to create additional methods 然后使用原型创建其他方法

myObjCreatedByFunction.prototype.func2 = function() { ... }

preferred over simple and plain creating object: 优先于简单和简单的创建对象:

//B)
var myObj = {
  prop1: "a",
  func1: function() {
   alert("a");
  }
};

and just adding new method by 并通过添加新方法

myObj.func2 = function() { ... }

as I see some skilled developers use the first approach and use prototypes even if they never create new instances later. 如我所见,即使某些技术开发人员以后不再创建新实例,他们也会使用第一种方法并使用原型。

Furthermore, is the use of IIFE preferred over both examples and if so, why? 此外,在两个示例中都优先使用IIFE,如果可以,为什么? (I see a lot of this usage lately) (最近我看到了很多这种用法)

var myObjIIFE = (function() {
return {
  prop1: "a",
  func1: function() {
   alert("a");
  }
}
})());

Can someone explain please when should I use first approach, second and third and why? 有人可以解释一下我什么时候应该使用第一种方法,第二种和第三种方法,为什么? Thank you! 谢谢!

B is useful only for a single instance. B仅对单个实例有用。 It does not offer a means to create multiple instances of that type with those methods and properties like A does. 它没有提供像A那样使用那些方法和属性来创建该类型的多个实例的方法。 If all you want/need is a single instance, then B is just fine and perhaps simpler to express and follow. 如果您想要/需要的只是一个实例,那么B就很好,并且可能更易于表达和遵循。

There are long discussions about whether to use the prototype for methods or assign them in the constructor (see prior reference and there are many, many other such discussions). 关于是将原型用于方法还是在构造函数中分配原型,已有很长的讨论(请参阅先前的参考资料 ,还有许多其他此类讨论)。 Using the prototype and then creating a new object using the new operator will conserve memory (since all methods are shared by all instances). 使用原型,然后使用new运算符创建新对象将节省内存(因为所有方法均由所有实例共享)。 Assigning them in the constructor allows access to private instance variables (the local variables in the constructor) and has slightly better performance when executing a given method. 在构造函数中分配它们可以访问私有实例变量(构造函数中的局部变量),并且在执行给定方法时具有更好的性能。

An IIFE is a pattern some people choose to adopt. IIFE是某些人选择采用的模式。 It offers no benefit for what you are doing, but does provide a scope to store "private class variables" which are shared by all instances, but not accessible to the outside world. 它没有为您的工作带来任何好处,但确实提供了一个存储“私有类变量”的范围,该变量由所有实例共享,但外界无法访问。 The way you show it, it is not my favorite because there is no type information on the object all. 显示它的方式并不是我的最爱,因为关于对象的所有信息都没有。 It's just a plain object with properties. 它只是具有属性的普通对象。 In some cases (particularly in debugging, but sometimes in coding), it can be useful to know what type of object you have. 在某些情况下(尤其是在调试中,但有时在编码中),了解您拥有的对象类型可能很有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM