Prototype and prototype chain

Constructor creates an object

Let's take a chestnut ========== =======

function Person (name) {// Constructors this.name = name;}

Person.prototype.printName = function () {// prototype object
Alert (this.name);}
var = new new PERSON1 the Person ( 'Ming'); // instantiate the object
console.log (person1 .__ proto__); // where print their own look, more impressive
console.log (person1.constructor); // console.log (Person.prototype); //
; var = new new PERSON2 the Person ( 'Frank')
example (note the case here) Person of person1 contains a name attribute, but also automatically generate a __proto__ attribute that points to the Person prototype, can be accessed printName method defined in the prototype, probably like this: each JavaScript function has a prototype property, this property references an object that is the prototype object . Prototype object initialization time is empty , we can customize any of the properties and methods in which these methods and properties will be the constructor of the created object inheritance . (Next I will be more detailed description of the prototype, very violent)

Examples are created by the constructor. Examples created to have a property constructor (the constructor points) and proto properties (prototype object point),

Constructor has a prototype property, this property is a pointer to its prototype object.

Internal prototype object also has a pointer (constructor property) directed constructor: Person.prototype.constructor = Person;

Examples can access properties and methods defined on the prototype object.

Here is an instance of person1 and person2, prototype is their prototype object.

// ======== =========== again a chestnut

function Animal (name) {// Constructor

this.name = name; //设置对象属性}

Animal.prototype.behavior = function() {

console.log("this is a "+this.name);

}
var Dog = new Animal("dog");
var Cat = new Animal("cat");

Dog.behavior (); // Dog object calls behavior by direct method

Cat.behavior(); //"this is a cat"

console.log(Dog.behavior==Cat.behavior);// true;

// Summary: The methods defined on the prototype constructor can do to directly call the object, and the code is shared. Animal prototype attribute points to the object.

================= start the topic ==================

-1-Prototype:

Each function has a prototype property, there are only a function of the property.

function Person(){ }

Person.prototype.name = 'name';

was PERSON1 = new Person ();

was person2 = new Person ();

console.log(person1.name) // name

console.log(person2.name) //name

Q: What's the function prototype property in the end point is it? Is the prototype of this function do?

| |
| |

Summary: Prototype prototype property points to a function object that it is calling the constructor created instance, it is the prototype in this example person1 and person2 of.

Q: What is a prototype? Each JavaScript object (except null) will be associated with another object when it is created, this object is what we call prototype, each object will "inherit" the attributes from the prototype.

-2-proto:

Represents an example of how the prototype and example, is the relationship between the person and Person.prototype it, this time we have talked about the second property:

proto. Each JavaScript object (except null) has a property called proto, this property will point to the prototype of the object.

function Person( ){

}

var person = new Person();

console.log(person.proto === Person.prototype); //true

Q: Since the instance object and constructors can point to a prototype, so if there is a prototype property points to a constructor or instance of it?

-3- constructor

It does not point to the example, because a constructor can generate a plurality of instances, however, some prototype point constructor, which would be talked about the third property: construcotr, each prototype has a constructor property associated point constructor

To verify this, we can try:

function Person() {

}

console.log(Person === Person.prototype.constructor); //true

A comprehensive summary of the essence ---- :( see big write-down)

function Person () {// Constructor

}

var person = new Person (); // instance of an object

console.log(person.proto == Person.prototype) //true

console.log (Person.prototype.constructor == Person) // true // way to learn a ES5, the object can be obtained prototype console.log (Object.getPrototypeOf (person) === Person.prototype) // true

Examples of the prototype

When reading the properties instance, if found, will look for the prototype associated with the object's attributes, if still finding out, went to prototype prototype, it has been found so far top level.

====== For example: =========

function Person() {

}

Person.prototype.name = 'name';

var person = new Person();

person.name = 'name of this person';

console.log(person.name) // name of this person

delete person.name;

console.log(person.name) // name

I set up a person's name property, so I can read as 'name of this person', when I removed the person's name attribute, read person.name, will not find the prototype from the person from the person that is person.proto == Person.prototype look, fortunately we found for the 'name', but the case has not found it? What prototype prototype of what is it?

Earlier, I said that the prototype is an object, since it is the object, I can use the most primitive way to create it, and that is

var obj = new Object (); // create two objects literally a new

obj.name = 'name';

console.log(obj.name) // name

So prototype object is generated by the Object constructor, spoken before binding, proto instance constructor prototype points,

Prototype chain

Q: What Object.prototype prototype is?

null, ah, yes, it is null, so you can stop found Object.prototype searched

Supplement, and finally, to supplement and correct in this article some of the loose places:

The first is the constructor,

function Person() {

}

var person = new Person();

console.log(person.constructor === Person); // true

When acquiring person.constructor, in fact person and not the constructor property, when the property can not be read to the constructor will is read first from person Person.prototype prototype, the prototype has just this property, so

person.constructor === Person.prototype.constructor

Guess you like

Origin www.cnblogs.com/xiaoniaohhl/p/11105459.html