Intanceof and the __proto__ and prototype

Source: Internet
Author: User

instanceof Operation code

1 functioninstance_of (L, R) {2 //L = left expression, R for right expression3  varO =R.prototype;4 //the display prototype of R is taken5L =l.__proto__;6 //an implicit prototype of L7   while(true) { 8    if(L = = =NULL) 9      return false; Ten    if(O = = =L) One //Here's the point: Returns True when O is strictly equal to L A      return true;  -L =l.__proto__; -  }  the}
var function (name) {    this. Name = name;}  var New Person (); The operation of the new operator is var p ==  Person.prototypePerson.call (p)

var p={}; In other words, initialize an object p.

p.__proto__ = Person.prototype;

Person.call (P); that is, construct p, which can also be called initialization p.

The key is the second step, and let's prove it:

var function () {}varnew=  person.prototype)

This piece of code returns TRUE. Explain the correctness of step 2.

So what is __proto__? Let's simply say here. Each object initializes a property inside it, that is, __proto__, when we access the property of an object, if the object does not exist inside this property, then he will go to __proto__ to find this attribute, this __proto__ will have their own __proto__, So we have been looking for the concept of the prototype chain we usually call.

According to the standard, __proto__ is not public, that is, a private property, but the engine of Firefox exposes him to become a common attribute, we can access and set up externally.

Well, the concept is clear, let's take a look at the following code:

var function  function  () {    alert ("person say");} var New

This code is very simple, I believe everyone has written this, so let's see why p can access the person's say.

First, var p=new person (); P.__proto__=person.prototype can be obtained. So when we call P. Say (), first there is no Say this attribute in P, so, he needs to go to his __proto__ to find, that is, person.prototype, and we define the above person.prototype.say=function () {}; So, we found this method.

OK, next, let's look at a more complicated one.

varperson =function () { }; Person.prototype.Say=function() {alert ("Person say");} Person.prototype.Salary= 50000;varProgrammer =function () { }; Programmer.prototype=NewPerson (); Programmer.prototype.WriteCode=function() {alert ("Programmer writes Code");}; Programmer.prototype.Salary= 500;varp =NewProgrammer ();p. Say ();p. Writecode (); alert (p.salary); 

Let's do this derivation:

var p=new Programmer () can be obtained p.__proto__=programmer.prototype;

And on top we specify programmer.prototype=new person (); Let's split this up, Var p1=new person (); PROGRAMMER.PROTOTYPE=P1; then:

P1.__proto__=person.prototype;

Programmer.prototype.__proto__=person.prototype;

Get P.__proto__=programmer.prototype on top of the above. Can get p.__proto__.__proto__=person.prototype.

Well, after figuring it out, let's look at the results above, P.say (). Because P does not say this attribute, so go to p.__proto__, that is, Programmer.prototype, that is, p1 to find, because there is no P1 say, then go to p.__proto__.__proto__, That is person.prototype to find, so found the alert ("Person say") method.

The rest is the same.

Transfer from http://www.cnblogs.com/zzcflying/archive/2012/07/20/2601112.html

Intanceof and the __proto__ and prototype

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.