search

Home  >  Q&A  >  body text

javascript - Js method to create objects

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?

迷茫迷茫2750 days ago713

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师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?

    reply
    0
  • Cancelreply