About the prototype chain, so simple? ----- End of those things and prototype of __proto__

Today, a technical group where the children asked a question:

Object.prototype.a = function () {
    console.log('a')
}

Function.prototype.b = function () {
    console.log('b')
}

function F(){}
var f = new F();

f.a();
f.b();
F.a();
F.b();

I silently universal formula in mind, the answer to what came out:

a;

报错(f.b is not a function);

a;

b;

Erupted in the topic and people had doubts, you are knocked out of the console it!

But in fact, the prototype chain is really very simple. Man of few words said, began performing!

First, we briefly explain to instantiate an object (new) in the end what has been done?

function Base () {
     the this II.A = function () { 
        the console.log ( 'I am in Base A' ) 
    } 
} 

Base.prototype.b = function () { 
    the console.log ( 'I b on the prototype Base ' ) 
} 

var obj = new new Base ();
 // actually doing following things 
// var obj = {}; 
// obj .__ proto__ = Base.prototype; 
// Base.call (obj); 
// the first line, we created an empty object obj 
// second line, we will __proto__ members of this empty object points to the member object prototype function objects Base 
// third line, we will Base member functions on this assigned to the obj

This console to test it:

Well, you can start problem solving, review the issues raised by children:

Object.prototype.a = function () { console.log('a') }
Function.prototype.b = function () { console.log('b') }
function F(){}
var f = new F();
f.a();f.b();F.a();F.b();

We launched the prototype chain of f:

=== .__ proto__ f F.prototype; 

// because the prototype is the nature of the object, inherited from Object, so .__ proto__ === Object.prototype F.prototype 
f .__ proto __.__ proto__ === Object.prototype; 

// Press one, will come Object.prototype .__ proto__ = Object.prototype, that would be endless, in the so js Object.prototype .__ proto_ directly assigned to null 
F .__ proto __.__ proto __.__ proto__ === null ;

We find that the prototype chain f in, no matter what Function.prototype, so the answer came out, fa () output a, fb will complain;

We expand the prototype chain F:

=== .__ proto__ F Function.prototype; 

// because the prototype is the nature of the object, inherited from Object, so .__ proto__ === Object.prototype Function.prototype 
F .__ proto __.__ proto__ === Object.prototype; 

f .__ proto__. __.__ proto__ __proto === null ;

OK, Function.prototype Object.prototype and the chain are in the prototype, the output will have, the answer is revealed, Fa of the () outputs a, Fb () Output B;

I mentioned earlier the universal formula, represent the masses eat melon, Is formula is this? Of course not, please listen to me carefully to have;

Just the story was over, I asked the children back to the whim of a problem, you just conditions Object.a (); Object.b (); Function.a (); Function.b (); What output?

Children answer: a; b; a; b;

Not bad, but he received a sentence (his 1 and 4 in fact wrong, the follow-up will tell the reasons):

1. Object.a (); Direct prototype value;

2. Object is Function.prototype instance, can be inherited B;

3. Function.__proto__ === Function.prototype;

4. Function.b (); Direct prototype value;

 

? ? ? Object.a first step directly onto Object.prototype looking for? This could also be a big problem we figure out the prototype chain, did not talk much, please spread the prototype chain of Object:

// Object is a function, inherited from Function 
Object .__ proto__ === Function.prototype; 

Object .__ proto __.__ proto__ === Object.prototype; 

Object .__ proto __.__ proto __.__ proto__ === null ;

Therefore, careful friend has found his mistake, Object.a actually have access to the Object .__ proto __.__ when proto__ found on the corresponding Object.prototype a ();

Also launched the prototype chain Function, will not repeat them:

// Function itself is also a function, and therefore inherit his 
Function .__ proto__ === Function.prototype; 

Function .__ proto __.__ proto__ === Object.prototype; 

Function .__ proto __.__ proto __.__ proto__ === null ;

In order to guide the universal formula, I again sent a tortured soul:

// 在刚才的前提下
var c = 1;
console.a();// a
console.b();// console.b is not a function
c.a();// a
c.b();// c.b is not a function
console.log.a();// a
console.log.b();// b

At this time, the students learn to expand the prototype chain already know the answer, I'll just tell my universal formula:

In the js, all things are subject, as long as the writing on the Object.prototype method, everything can be accessed; and the function method on Function.prototype, only by () method call in order to access;

So I just need to see this object can you call in the form of function (), and will be able to determine whether he can access Function.prototype. Recalling again the topic people's problems, f is just an object, f () will report not is a function of the error, and F () is a function that can be called, all of a sudden come to a given ab answer is not learned Bang Bang da ~~~ feel included

The final Cober teacher or gives his killer interview questions, interview after the prototype chain There is another method:

= Object.prototype.a function () { 
    the console.log ( 'I am in Object A' ) 
} 
Object.prototype.b = function () { 
    the console.log ( 'I am in Object B' ) 
} 
Function. prototype.a = function () { 
    the console.log ( 'I function in a' ) 
} 
Function.prototype.b = function () { 
    the console.log ( 'I am in the function B' ) 
} 
function F. () {
     the this II.A = function () { 
        the console.log ( 'I F is a')
    }
}
F.prototype.a = function () {
    console.log('我是F的prototype中的a')
}
var f = new F();
f.a();f.b();F.a();F.b();Object.a();Object.b();Function.a();Function.b();

(Note: the call sequence is a prototype chain fa -> f .__ proto __ a -> f .__ proto __.__ proto __ a, until access to null..)

Guess you like

Origin www.cnblogs.com/kbaoq/p/11354549.html