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?
天蓬老师2017-05-19 10:27:57
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?