Chinaunix首页 | 论坛 | 博客
  • 博客访问: 81965
  • 博文数量: 26
  • 博客积分: 547
  • 博客等级: 中士
  • 技术积分: 243
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 23:14
文章分类

全部博文(26)

文章存档

2012年(10)

2011年(16)

分类: 系统运维

2012-02-21 10:13:33

JavaScript 中通过call或者apply用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
简单的说就是改变函数执行的上下文,这是最基本的用法。两个方法基本区别在于传参不同。

call(obj,arg1,arg2,arg3);call第一个参数传对象,可以是null。参数以逗号分开进行传值,参数可以是任何类型。

  1. //类的继承
  2. function person(name,age){
  3. this.name = name;
  4. this.age = age;
  5. this.alertName = function(){
  6. alert(this.name);
  7. };
  8. this.alertAge = function(){
  9. alert(this.age);
  10. }
  11. }
  12. function inherit(name,age,***){
  13. person.call(this,name,age);
  14. this.*** = ***;
  15. this.alert*** = function(){
  16. alert(this.***);
  17. }
  18. }
  19. var test = new inherit("铁骑如风",26,"男");
  20. test.alertName(); //铁骑如风
  21. test.alertAge(); //26
  22. test.alert***(); //男



阅读(865) | 评论(0) | 转发(0) |
0

上一篇:基于电子商务的lamp环境

下一篇:JS绝句

给主人留下些什么吧!~~