实例与原型的链条(原型链的基本概念)

53 阅读1分钟
  // 构造函数SuperType
  function SuperType(){
    console.log(this// 指向SuperType
    this.property = {  // 构造函数的 属性值
      name:"Lyon",
      say:function(){
        console.log(this.name);
        return this;
      }
    };
  }
  SuperType.prototype.getSuperValue = function(){
    return this.property;
  }
  /// 构造函数SuberType;
  function SubType(){
    this.subpropertyfalse;
  }
Tip思考:
1.  继承SuperType  将一个原型对象指向 另一个  实例对象** SubType 继承了 SuperType,而继承是通过创建 SuperType 的实例,并将该实例赋给,代之以一个新类型的实例。换句话说,原

来存在于 SuperType 的实例中的所有属性和方法,现在也存在于 SubType.prototype 中了。 SubType.prototype 实现的。实现的本质是重写原型对象SubType.prototype 中的 constructor 被重写了的缘故(指向的是 SuperType)。调用instance.getSuperValue()会经历三个搜索步骤:1)搜索实例;2)搜索 SubType.prototype; 3)搜索 SuperType.prototype

  SubType.prototype = new SuperType();
  SubType.prototype.getSubValue = function(){
    return this.subproperty;
  }
  var instance = new SubType();
  console.log(instance);
  console.log(instance.getSuperValue());  //   返回值如下
   /*** // 构造函数的 属性值
      name:"Lyon",
      say:function(){
        console.log(this.name
        }    *****// 
  console.log(instance.getSuperValue().say()); // Lyon