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?
为了避免太复杂,你这么理解吧:
构造函数模式:
var person = new Person('hello')
工厂模式:
var person = creatPerson('hello')
至于 prototype 的问题,参考我另一个回答吧:javascript是面向对象的,怎么体现javascript的继承关系?