javaScript——原型繼承四步曲

<script>
  //js模擬類的建立以及繼承

  //第一步:建立父類
  function Parent(name){
  this.name = name;
  }this


  //給父類添加屬性方法
  Parent.prototype.age = 18;
  //var p1 = new Parent();
  //第二步:建立子類
  function Child(){
    Parent.call(this,"asdfasfd");
  }

  //第三步:肯定繼承的關係
  Child.prototype = Object.create(Parent.prototype);

  Child.prototype.stuno = "2000";

  //第四步:改造構造器(不是很重要)
  //改變了某個構造器的原型以後,緊接着的代碼必定是改構造器
  Child.prototype.constructor = Child;
  /* Object.create的實現
  function create(proto){
  function F(){}
  F.prototype = proto;
  var temp = new F();
  return temp;
  }

  */
  //var o = new Parent();
  //o instanceof Object;


  </script>prototype

相關文章
相關標籤/搜索