理解 javascript prototype

2016-08-12  本文已影响40人  并肩走天涯

javascript prototype

  1. prototype 的含义
  1. 验证
function base() {
  this.show = function() {
    console.log("base:show");
  }
}
function extend() {
  this.show = function() {
    console.log("extend:show");
  }
}
extend.prototype = new base();
var instance = new extend();
instance.show(); // extend:show
var base = new base();
base.show.call(instance); // base:show

函数运行时,会先去本体的函数中查找,如果找到则运行,否则到prototype中查找函数。或者可以理解为prototype不会克隆同名的函数。

上一篇 下一篇

猜你喜欢

热点阅读