javascript - Js method to create objects
迷茫
迷茫 2017-05-19 10:25:57
0
1
741

The advanced tutorial mentioned that methods for creating objects include literal method, factory mode, constructor mode, prototype mode, mixed mode, etc. There is an example in the factory pattern,

function  creatPerson(name){
   var  obj=new Object();
   obj.name=name;
   return  obj;
}
var person=creatPerson(“hello”);

The constructor pattern has been used inside the function, so why is the factory pattern still present? Are these methods of creating objects developed step by step? Is the prototype attribute of a function only for the prototype mode, or has it existed since the birth of JavaScript? Can anyone tell me the history?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
Peter_Zhu

In order to avoid too much complexity, you can understand it this way:

  • Constructor Pattern: var person = new Person('hello')

  • Factory mode: var person = creatPerson('hello')

As for the prototype problem, please refer to my other answer: JavaScript is object-oriented, how to reflect the inheritance relationship of JavaScript?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template