javascript继承:javascript 类继承

="t18">1.第种方式,冒充对象方式.(利用js里个思路方法名都是个Function对象) Java代码
  function Parent(username){
  this.username = username;
  this.say = function{
  alert(this.username);
  }
  }
  function Child(username,password){
  this.temp = Parent;//temp指向Parent所指向地方 利用js里个思路方法名都是个Function对象,指向个思路方法
  this.temp(username);//化思路方法里内容
  delete this.temp;//temp没有用了可以直接删除掉.this不可以丢了
  //Parent(username);//这样写表面看起来是正确其实是只有出来对象才有this,所以Parent里this就没有值了 
  this.password = password;
  this.hello = function{
  alert(this.password);
  }
  }
  var parent = Parent("zhangsan");
  parent.say;//zhangsan
  var child = Child("lisi","123456");
  child.say;//lisi
  child.hello;//123456
  //第种方式,冒充对象方式.(利用js里个思路方法名都是个Function对象)
  function Parent(username){
  this.username = username;
  this.say = function{
  alert(this.username);
  }
  }
  function Child(username,password){
  this.temp = Parent;//temp指向Parent所指向地方 利用js里个思路方法名都是个Function对象,指向个思路方法
  this.temp(username);//化思路方法里内容
  delete this.temp;//temp没有用了可以直接删除掉.this不可以丢了
  //Parent(username);//这样写表面看起来是正确其实是只有出来对象才有this,所以Parent里this就没有值了
  
  this.password = password;
  this.hello = function{
  alert(this.password);
  }
  }
  var parent = Parent("zhangsan");
  parent.say;//zhangsan
  var child = Child("lisi","123456");
  child.say;//lisi
  child.hello;//123456
  2.第 2种方式:call方式 call是Function对象
  具体使用方法如下 Java代码
  //call是Function对象
  //具体使用方法如
  function test(str){
  alert(this.username + "," + str);
  }
  var o = Object;
  o.username = "zhangsan";
  test.call(o,"123456");//zhangsan,123456 .每个Function 对象都有个call思路方法名就是个Function对象.call个参数是testthis.
  //call是Function对象
  //具体使用方法如
  function test(str){
  alert(this.username + "," + str);
  }
  var o = Object;
  o.username = "zhangsan";
  test.call(o,"123456");//zhangsan,123456 .每个Function 对象都有个call思路方法名就是个Function对象.call个参数是testthis.
  Java代码
  function Parent(username){
  this.username = username;
  this.say = function{
  alert(this.username);
  }
  }
  function Child(username,password){
  Parent.call(this,username);
  this.password = password;
  this.hello = function{
  alert(this.password);
  }
  }
  var parent = Parent("zhangsan");
  parent.say;//zhangsan
  var child = Child("lisi","123456");
  child.say;//lisi
  child.hello;//123456
  function Parent(username){
  this.username = username;
  this.say = function{
  alert(this.username);
  }
  }
  function Child(username,password){
  Parent.call(this,username);
  this.password = password;
  this.hello = function{
  alert(this.password);
  }
  }
  var parent = Parent("zhangsan");
  parent.say;//zhangsan
  var child = Child("lisi","123456");
  child.say;//lisi
  child.hello;//123456
  3.第 3种实现方式:apply方式
  apply和call只不过参数传递区别而已,apply参数为 Java代码
  //第 3种实现方式:apply方式apply和call只不过参数传递区别而已,apply参数为
  //所以继承可以这样实现
  function Parent(username){
  this.username = username;
  this.say = function{
  alert(this.username);
  }
  }
  function Child(username,password){
  Parent.apply(this, Array(username));
  this.password = password;
  this.hello = function{
  alert(this.password);
  }
  }
  var parent = Parent("zhangsan");
  parent.say;//zhangsan
  var child = Child("lisi","123456");
  child.say;//lisi
  child.hello;//123456
Tags:  javascript javascript创建类 javascript类定义 javascript继承

延伸阅读

最新评论

发表评论