First, the object
1.
var person={
Name: "Kobe",//key value must be used, interval
"Age": 30,
Sayname:function () {
Console.log ("I ' M" +this.name);
}
};
Configurable : Indicates whether the property can be modified, deleted
can delete Configurable:true/false
Delete is invalid if Configurable:false
can modify Writable:true/false
enumerated Enumerable:true/false
var person2={
};
DefineProperty is the object provided by the party to add the Name property to the Person2 object
Object.defineproperty (Person2, "name", {
configurable:true,//can be removed by default to True
writable:true,//can be modified by default to True
enumerable:true,//can be traversed by default to True
Value: "Kobe"
});
Second, create the object
JSON object
var person={
Name: "Alice",
Age:30,
Sayname:function () {
Console.log (this.name);
}
};
Mode 2
var person={};//creating an Object
Person.name= "Alice";
person.age=30;
Person.sayname=function () {
Console.log (this.name);
};
Console.log (Person.name);
Console.log (Person.age);
Person.sayname ();
Function
Mode 1 Returns the JSON object
function Getperson (name,age) {
return{
Name:name,
Age:age,
Sayname:function () {
Console.log (this.name);
}
};
}
var Person=getperson ("Alice", 30);
Console.log (Person.name);
Console.log (Person.age);
Person.sayname ();
If return is empty
Person.name= "Alice";
person.age=30;
Mode 2 Returns an object
function Getperson (name,age) {
var obj=new Object ();
Obj.name=name;
Obj.age=age;
Obj.sayname=function () {
Console.log (this.name);
};
return obj;
}
var P=getperson ("Alice", 30);
Console.log (P.name);
Console.log (P.age);
P.sayname ();
var P2 = Rgetperson ("Joe", 45);
Console.log (P2.name);
Console.log (P2.age);
P2.sayname ();
Constructor class class name initial capitalization will have an implicit return, do not need to return with this create object with new
function Person (name,age) {
this.name=name;//with this
This.age=age;
This.sayname=function () {
Console.log (this.name);
}
}
var person=new person ("Alice", 30);//create object with new
Console.log (Person.name);
Console.log (Person.age);
Person.sayname ();
Iii. prototype prototype (properties of the function)
(Each object has a prototype property, and its properties are the type of the object)
Define attribute sharing within a prototype as long as any one object changes, the rest will change.
function per () {
This.name= "Zhang San";
}
per.prototype.arr=[1,2,3,4,5];
var p1=new per ();
var p2=new per ();
P1.arr.push (6);
Console.log (P1.arr);//[1,2,3,4,5,6]
Console.log (P2.arr);//[1,2,3,4,5,6]
Four, the implicit prototype __proto__
_proto_ is a property of an object each object has a ___proto___ property that points to the prototype of the function that created the object
function person () {}
var p1=new person ();
Console.log (p1.__proto__);
Console.log (Person.prototype);
Console.log (P1.__proto__===person.prototype); The __proto__ property of the//true object is the same as the prototype result of the function
Five prototype chain
1, there is a prototype property in the function, the property is an object, the object has a default constructor. It points to its own method (constructor)
2. The object has a __proto__ property whose value points to the prototype of the function
The prototype chain can inherit the properties and methods of its prototype and does not support multiple inheritance
Parent class
function Parent (name,age) {
This.name=name;
This.age=age;
This.sayname=function () {
Console.log (this.name);
}
}
To specify a prototype for parent
Parent.prototype.sayage=function () {
Console.log (This.age);
};
Subclass child can add its own attributes such as sex
function Child (Name,age,sex) {
This.constructor (name, age);//constructor is the default attribute to point to obj
This.sex=sex;
}
Child.prototype=new parent ();//Make child point to parent's prototype by means of a prototype chain
var s=new child ("Alice", 30);
S.sayname ();
S.sayage ();
# # impersonating # # can only inherit its parent's original properties and methods, but its prototype properties and methods cannot inherit
function Child2 (name, age) {
This.obj = parent;//posing
This.obj (name, age);//Inheritance
Delete this.obj;//Remove inheritance
Parent.call (this, name, age);
Parent.apply (this, [name, Age]);//apply is an array, similar to the call method
}
var c = new Child2 ("Zhangsan", 30);
C.sayage ();//method that illustrates a prototype that cannot be inherited
C.sayname ();
Console.log (C.age);
Defining constructors Worker
function Worker (pay,work) {
This.pay=pay;
This.work=work;
}
Object impersonation support multi-Inheritance a class can inherit multiple superclass
function Fworker (name,age,pay,work) {
this.inherit=people;//impersonating People class
This.inherit (Name,age);
Delete This.inherit;
this.inherit=worker;//impersonating a Worker class
This.inherit (pay,work);
Delete This.inherit;
}
Test
var john=new fworker ("John", "$1000", "Teacher");
John.say ();
Console.log (john.work);
Description
1. The prototype chain can inherit its original properties, and methods, can only specify a parent class (prototype), but does not support multiple inheritance
2. Inheritance, impersonation can only inherit its original properties and methods, unable to inherit its prototype properties and methods
3. The difference between a hybrid inheritance and a prototype chain is where a subclass inherits his attributes by impersonating an object.
Object Prototype prototype