Prototype and distal] JS [prototype chain

A common object and the function object

JS objects into objects and functions of ordinary objects, Object Function and function objects are built JS.

var o1 = {}; 
var o2 =new Object();
var o3 = new f1();  // 这个地还不大理解

function f1(){}; 
var f2 = function(){};
var f3 = new Function('str','console.log(str)');

console.log(typeof Object); //function 
console.log(typeof Function); //function  

console.log(typeof f1); //function 
console.log(typeof f2); //function 
console.log(typeof f3); //function   

console.log(typeof o1); //object 
console.log(typeof o2); //object 
console.log(typeof o3); //object

Object () created by the new Function is a function of the object, the other is an ordinary object. f1, f2, in the final analysis is through new Function () way to create. Function Object are also by New Function () created

Two constructors

Attribute instance constructor (constructor) pointing constructor.

function Person(name, age, job) {
 this.name = name;
 this.age = age;
 this.job = job;
 this.sayName = function() { alert(this.name) } 
}
var person1 = new Person('Zaxlct', 28, 'Software Engineer');
var person2 = new Person('Mick', 23, 'Doctor');
console.log(person1.constructor == Person); //true
console.log(person2.constructor == Person); //true

person1, person2 and no constructor, is looking for in the parent class.
person1 and person2 are the Person instance constructor.

Triarch objects

Each object has _ proto _ attribute
only function object has a prototype property that points to the function prototype object, the prototype property of the object is equivalent to the default property of the object.

function Person() {}
Person.prototype.name = 'Zaxlct';
Person.prototype.age  = 28;
Person.prototype.job  = 'Software Engineer';
Person.prototype.sayName = function() {
  alert(this.name);
}
  
var person1 = new Person();
person1.sayName(); // 'Zaxlct'

var person2 = new Person();
person2.sayName(); // 'Zaxlct'

console.log(person1.sayName == person2.sayName); //true

Prototype object also has a default constructor property

Four _ Proto _

JS when you create an object (whether ordinary object or function object), there is a built-in properties named __proto__ for point to create its prototype object constructor.
Person1 __proto__ objects have a property, its constructor is to create a prototype object Person, constructor is Person.prototype, so:

person1.__proto__ == Person.prototype
Person.prototype.constructor == Person;
person1.__proto__ == Person.prototype;
person1.constructor == Person;

However, to clear the really important point is that this connection exists between instances (person1) constructor (Person) prototype object (Person.prototype), rather than exist in instances (person1) constructor (Person) between.

Five constructors

Guess you like

Origin blog.csdn.net/cheidou123/article/details/93376969