欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 3220 次 点评:59

java,


1) 对象冒充 

[html] view plain copy print?

  1. //继承第一种方式:对象冒充  

  2.   

  3. function Parent(username)  

  4. {  

  5.     this.username = username;  

  6.   

  7.     this.sayHello = function()  

  8.     {  

  9.         alert(this.username);  

  10.     }  

  11. }  

  12.   

  13. function Child(username, password)  

  14. {  

  15.     //下面三行代码是最关键的代码  

  16.     this.method = Parent;  

  17.     this.method(username);  

  18.     delete this.method;  

  19.   

  20.     this.password = password;  

  21.   

  22.     this.sayWorld = function()  

  23.     {  

  24.         alert(this.password);  

  25.     }  

  26. }  

  27.   

  28. var parent = new Parent("zhangsan");  

  29. var child = new Child("lisi", "1234");  

  30.   

  31. parent.sayHello();  

  32.   

  33. child.sayHello();  

  34. child.sayWorld();  


2) call方法方式。 

call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。 

[html] view plain copy print?

  1. <p>//使用call方式实现对象的继承</p><p>function Parent(username)  

  2. {  

  3.  this.username = username;</p><p> this.sayHello = function()  

  4.  {  

  5.   alert(this.username);  

  6.  }  

  7. }</p><p>function Child(username, password)  

  8. {  

  9.  Parent.call(this, username);</p><p> this.password = password;</p><p> this.sayWorld = function()  

  10.  {  

  11.   alert(this.password);  

  12.  }  

  13. }</p><p>var parent = new Parent("zhangsan");</p><p>var child = new Child("lisi", "123");</p><p>parent.sayHello();</p><p>child.sayHello();  

  14. child.sayWorld();</p><p>  

  15.  </p><span style="font-size:18px;"><span style="color:#000000;"> </span></span>  


 1) 对象冒充 

[html] view plain copy print?

  1. //继承第一种方式:对象冒充  

  2.   

  3. function Parent(username)  

  4. {  

  5.     this.username = username;  

  6.   

  7.     this.sayHello = function()  

  8.     {  

  9.         alert(this.username);  

  10.     }  

  11. }  

  12.   

  13. function Child(username, password)  

  14. {  

  15.     //下面三行代码是最关键的代码  

  16.     this.method = Parent;  

  17.     this.method(username);  

  18.     delete this.method;  

  19.   

  20.     this.password = password;  

  21.   

  22.     this.sayWorld = function()  

  23.     {  

  24.         alert(this.password);  

  25.     }  

  26. }  

  27.   

  28. var parent = new Parent("zhangsan");  

  29. var child = new Child("lisi", "1234");  

  30.   

  31. parent.sayHello();  

  32.   

  33. child.sayHello();  

  34. child.sayWorld();  


2) call方法方式。 

call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。 

[html] view plain copy print?

  1. <p>//使用call方式实现对象的继承</p><p>function Parent(username)  

  2. {  

  3.  this.username = username;</p><p> this.sayHello = function()  

  4.  {  

  5.   alert(this.username);  

  6.  }  

  7. }</p><p>function Child(username, password)  

  8. {  

  9.  Parent.call(this, username);</p><p> this.password = password;</p><p> this.sayWorld = function()  

  10.  {  

  11.   alert(this.password);  

  12.  }  

  13. }</p><p>var parent = new Parent("zhangsan");</p><p>var child = new Child("lisi", "123");</p><p>parent.sayHello();</p><p>child.sayHello();  

  14. child.sayWorld();</p><p>  

  15.  </p><span style="font-size:18px;"><span style="color:#000000;"> </span></span>

  16. 好好学习天天向继续努力吧。

    JavaScript中的继承。 

    1) 对象冒充 

    [html] view plain copy print?

    1. //继承第一种方式:对象冒充  

    2.   

    3. function Parent(username)  

    4. {  

    5.     this.username = username;  

    6.   

    7.     this.sayHello = function()  

    8.     {  

    9.         alert(this.username);  

    10.     }  

    11. }  

    12.   

    13. function Child(username, password)  

    14. {  

    15.     //下面三行代码是最关键的代码  

    16.     this.method = Parent;  

    17.     this.method(username);  

    18.     delete this.method;  

    19.   

    20.     this.password = password;  

    21.   

    22.     this.sayWorld = function()  

    23.     {  

    24.         alert(this.password);  

    25.     }  

    26. }  

    27.   

    28. var parent = new Parent("zhangsan");  

    29. var child = new Child("lisi", "1234");  

    30.   

    31. parent.sayHello();  

    32.   

    33. child.sayHello();  

    34. child.sayWorld();  


    2) call方法方式。 

    call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。 

    [html] view plain copy print?

    1. <p>//使用call方式实现对象的继承</p><p>function Parent(username)  

    2. {  

    3.  this.username = username;</p><p> this.sayHello = function()  

    4.  {  

    5.   alert(this.username);  

    6.  }  

    7. }</p><p>function Child(username, password)  

    8. {  

    9.  Parent.call(this, username);</p><p> this.password = password;</p><p> this.sayWorld = function()  

    10.  {  

    11.   alert(this.password);  

    12.  }  

    13. }</p><p>var parent = new Parent("zhangsan");</p><p>var child = new Child("lisi", "123");</p><p>parent.sayHello();</p><p>child.sayHello();  

    14. child.sayWorld();</p><p>  

    15.  </p><span style="font-size:18px;"><span style="color:#000000;"> </span></span>  

相关文章

    暂无相关文章
相关栏目:

用户点评