<script>
  //js模拟类的创建以及继承

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


  //给父类添加属性方法
  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>

相关文章:

  • 2022-12-23
  • 2021-04-21
  • 2021-11-06
  • 2021-09-22
  • 2021-10-18
  • 2021-06-19
  • 2021-05-31
猜你喜欢
  • 2022-01-16
  • 2021-05-27
  • 2021-06-05
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
相关资源
相似解决方案