1. Simple Factory mode : Unable to determine object type
2. Constructor mode : Problems with private words
3. Prototype mode
//1 Simple Factory mode
function Creatperson (name,age,sex) {
var obj=new Object ();
Obj.name=name;
Obj.age=age;
Obj.sex=sex;
obj.sayname=function () {
Console.log ("Name:" +this.name+ "Age:" +this.age+ "Gender:" +this.sex ");
};
return obj;
}
var Person1=creatperson ("Iwen", 20, "man");
person1.sayname ();
var Person2=creatperson ("IME", 22, "woman");
person1.sayname ();
/*
* Function Createperson is able to eliminate incoming parameters and construct different objects, and can save the necessary information of the object
* The information stored on each object is independent.
* Question: Can the Person1 and Person2 objects know what the type is? Who does he belong to?
*
* typeof instanceof
*
* */
/*
*
* Constructor Mode
* in JavaScript, constructors can be used to create objects of a specific type
* Object,array itself is created using the constructor pattern .
*
* */
function Person (name,age,sex) {
this.name = name;
this.age = age;
this.sex =sex;
this.sayname = function () {
Console.log ("Name:" +this.name+ "Age:" +this.age+ "Gender:" +this.sex ");
}
}
var person1 = new Person ("Iwen", "19.8", "male");
var person2= new person ("IME", "200.8", "male");
person1.sayname ();
person2.sayname ();
//* In the current example, the person replaces the code in the Createperson,person
In addition to the same parts as the Createperson, different points
//* 1. Create objects that are not displayed
//* 2. Assign properties and methods directly to the This object
//* 3. No return object
// *
//* In the constructor, we need to use the keyword new to create the object
//* When we create an object from new, we go through four steps
//* 1. Create a new object
//* 2. Assigning the scope of a constructor to a new object
//* 3. Execute the code in the constructor (add properties and methods for this new object)
//* 4. Return the new object
// *
//* Person1 and Person2 two objects have a constructor (constructor), and this property points to the person
// *
// *
//* Only constructors exist constructor
//
//
//* Any function that is called by the keyword new can be seen as a constructor pattern
//* Any function can also be called in the form of an assignment
/*
*
* Prototype mode:
*
* */
function Person (name,age) {
this.name = name; Private properties and Methods
this.age = age;
this.oldsayname = function () {
}
}
//Not mounted on window, but mounted on prototype body
Person.prototype.sayName = function () {//Public method
Console.log ("Name:" +this.name+ "Age:" +this.age);
};
var person1 = new Person ("Iwen", "a");
var person2 = new Person ("Heihie", "+");
//Person1.sayname ();
//Person2.sayname ();
Person.prototype.sex = "male";
person1.sex = "female";
Console.log (person1.sex);
Console.log (person2.sex);
Console.log (person1.sayname = = = Person2.sayname);
Console.log (person1.oldsayname = = = Person2.oldsayname);
Console.log (person1.sex = = = Person2.sex);
//Prototype mode still has some problems, if there are multiple prototype chains, you can continue to optimize
function Fn (num1,num2) {
this.num1=num1;
this.num2=num2;
}
var Pro=fn.prototype;
Console.log (pro);
fn.prototype={
num3:function () {
Console.log ("I am Three");
},
num4:function () {
Console.log ("I am Four");
},
reset:function () {
For (Var key in pro) {
This[key]=pro[key];
}
},
CONSTRUCTOR:FN
};
Console.log (fn.prototype);
Simple Factory mode, constructor mode, prototype mode correlation