After understanding the object, I also understand the commonly used ways to create objects-constructor and literal, so in addition to this, what other ways to create objects?
factory pattern
To put it bluntly, it is a package function , which defines the details of the object inside the function, and finally returns it.
function createObj(name,job,age){ var obj = new Object(); obj.name = name; obj.job = job; obj.age = age; obj.logAge = function(){ console.log(this.age)//When the function is executed, it is executed in the object, so this points to the object itself } return obj } console.log(createObj("Zhang San", "Xiao Bai", 23))//It is an object; var person = createObj("Li Si","Accountant",23);//person saves a pointer to the newly created object; var person1 = person;//point to the same object console.log(person.logAge);//23 console.log(person1);//It's still an object
Constructor pattern
a). What is created by the constructor is an object,
b). This inside the constructor points to the new instance object;
c). Instance.constructor is a pointer to the creator of the instance - the constructor;
d). In the final analysis, a constructor is also a function, but the calling method is different. Of course, as a function, it can also be called like a normal function, but the result is different; the same is true for a normal function, which can also be used as a constructor call like that.
function CreateObj(name, job, age){ // var hobby = "compose poetry"; this.name = name; this.job = job; this.age = age ; this.logAge = function(){ console.log(this.age) } } var person_new = new CreateObj("Wang Wu", "Finance", 27);//person_new is an object; var person_new1 = new CreateObj("Wu Liu", "restaurant owner", 46);//person_new is an object; person_new.logAge();//27 person_new1.logAge()//46; console.log(person_new.constructor)//The constructor itself; console.log(person_new1 instanceof Object)//Because after all, person_new1 is also an object! //The constructor is called as a normal function; CreateObj("Li Bai","Poet",48);//this points to window at this time, because it is called in the global environment; console.log(name)//Li Bai
1. Constructor.prototype (prototype) : Each function has this property, which is a pointer to an object (prototype object) that contains properties and methods common to all instance objects created by the constructor; and At the same time, the prototype object also contains a constructor property (a pointer to the constructor that owns the prototype object)
2. Instance.__proto__: After creating an instance by constructing the constructor, the instance has an attribute __proto__, which is a pointer to the prototype object of the constructor . It is equivalent to, instance.__proto__ = constructor.prototype;
function Plant(height,classify){ this.height = height; this.class = classify; } Plant.prototype.name = "Rice";//All future instances will have the attribute name, the value is "Rice" console.warn(Plant.prototype.constructor)//The constructor itself; Plant.prototype.constructor.prototype.loca = "Henan·Xuchang"// console.warn(Plant.prototype.constructor)//The constructor itself; var plant_xiaomai = new Plant(137,"Dry season crops");//Instance of constructor console.log(plant_xiaomai);//An object console.log(plant_xiaomai.name)//水稻 console.log(plant_xiaomai.loca);//Henan. Xuchang console.log(plant_xiaomai.__proto__)//prototype object