Problems with prototype chain inheritance

question: Function A () {this.fn} what if B. prototype = new A () prototype chain inherits, it will pollute the variable space of B, for example, there is fn in B?

Mar.24,2021

there is no pollution, except that the object b created by constructor B has the attribute fn, and its prototype chain also has the fn attribute.

at this time, the b.fn accesses the fn property of the b object itself, and the fn property of the parent object can be accessed using b.__proto__.fn

.

to determine whether the method or value exists in B, it will look it up step by step according to its prototype chain and return the nearest one.

such as

    The prototype of
  • A containing fn,B points to Aline B and there is also fn
    so B.fn gets the fn of B
  • .
  • A's prototype containing fn,B points to A code B without fn
    , so B.fn actually gets A's fn
  • .
  • A has no fn, but A's prototype has, B's prototype points to A code B without fn
    , then B.fn actually gets the fn of A's prototype
  • .
  • An and prototype without fn,B point to A code B without fn
    then B.fn will consistently trace back to null, and return undefined

I also came across such topic during the interview at that time. Please refer to

.

after creating the example of B in new (assuming b), calling b.fn uses the this.fn defined by B instead of the this.fn defined by A. Continue to look for prototype objects only if they do not exist in B

function B() {
  this.fn = function() {
    console.log('B fn');
  }
}
function A() {
  this.fn = function() {
    console.log('A fn');
  }
  this.fn1 = function() {
    console.log('A fn1');
  }
}
B.prototype = new A();
var b = new B();
b.fn();
b.fn1();

// 
b fn
a fn1

if you can't find it in your scope, you will follow the prototype chain. It doesn't matter if you have the same name. Use whoever you find first

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35037-2bf41.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35037-2bf41.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?