1.Call方法

调用一个对象的一个方法,以另一个对象替换当前对象。
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
  • thisObj 可选项。将被用作当前对象的对象。
  • arg1, arg2, , argN 可选项。将被传递方法参数序列。

说明
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj
指定的新对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。

function person(name,age,sex){  //定义person
this.name=name;
this.age=age;
this.sex=sex;
}
function student(name,age,sex,university,major){ //定义Student
this.university=university;
this.major=major;
person.call(this,name,age,sex); //person.call(student,argument...)
} var john=new student("john",20,"male","MIT","webdeveloper"); //添加实例
alert(john.age); //显示结果为20

从上面的函数看,按理说student并没有age这个属性,但为什么john.age会是20?原因就是 person.call(student,name,age,sex);从结果看,call多少有点继承的味道。 A.call(B,argument,argument2...),结果就是B继承了A,对B进行实例化后,B里面继承了A的属性和方法。

function person(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
this.say=function(){alert("my name is "+name);}
}
function student(name,age,sex,university,major){
this.university=university;
this.major=major;
person.call(this,name,age,sex);
}
var john=new student("john",20,"male","MIT","webdeveloper");
alert(john.age);
john.say(); //调用say()方法,显示my name is john

只有方法才call方法,其他对象或属性没有,这里指的方法即typeof(any)==’function’,其他typeof非function的都没有call方法,比如Array数组就没有call方法。

稍微改动一下:

function person(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
this.say=function(){alert("my name is "+this.name);}
}
function student(name,age,sex,university,major){
this.name="inner";
this.university=university;
this.major=major;
}
var john=new student("john",20,"male","MIT","webdeveloper");
var person2=new person();
person2.say.call(john); //显示结果为my name is inner

person2.say.call(john),其实就是让john对象调用person2的say方法,而john对象里面this.name="inner",john调用perosn2的say方法的时候,关键字变量this.name会被替换,所以结果为:my name is inner。

同时继承多个类:

function parent(father,mother){
  this.father=father;
  this.mother=mother;
  this.tell=function(){alert(this.father+","+this.mother)}
}
function person(name,age,sex){
  this.name=name;
  this.age=age;
  this.sex=sex;
  this.say=function(){alert("my name is "+this.name);}
}
function student(name,age,sex,university,major){
  this.name="inner";
  this.university=university;
  this.major=major;
  parent.call(this);
  person.call(this,name,age,sex);
}
var john=new student("john",20,"male","MIT","webdeveloper");
alert(john.sex);
john.father="Bob John";
john.mother="Mary John";
john.tell(); //显示结果Bob John,Mary John
通过parent.call(this);person.call(this); student同时继承了parent和person的属性及方法。
2.Apply方法

应用某一对象的一个方法,用另一个对象替换当前对象。
apply([thisObj[,argArray]])

  • thisObj 可选项。将被用作当前对象的对象。
  • argArray 可选项。将被传递给该函数的参数数组。

说明
如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。
如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj,
并且无法被传递任何参数。
Apply方法与Call的区别就是在于,call传递参数直接列出来就可以了,而Apply传递参数需要把参数放入数组里面。

function person(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
this.say=function(){alert("my name is "+this.name);}
}
function student(name,age,sex,university,major){
this.university=university;
this.major=major;
person.apply(this,[name,age,sex]); //apply与call的区别,传递参数的方法不同
}
var john=new student("john",20,"male","MIT","webdeveloper");
alert(john.sex); //显示male
john.say(); //显示my name is john

3.argument对象

argument是JavaScript的内置对象,它代表正在执行的函数和调用它的函数的参数。

[function.]arguments[n ]

  • 其中function是可选项。当前正在执行的 Function 对象的名字。
  • n是必选项。要传递给 Function 对象的从0开始的参数值索引。

不能显式创建arguments对象。arguments对象只有函数开始时才可用。函数的arguments对象并不是一个数组,访问单个参数的方式与访问数组元素的方式相同。索引n实际上是arguments对象的0…n属性的其中一个参数。

<script language="javascript" type="text/javascript">(function argTest(a,b,c,d){
alert("函数需要 "+argTest.length+" 个参数");
alert("已经传入的参数为:"+arguments.length+"个");
document.writeln("参数分别为:");
for(var i=0;i<arguments.length;i++){
document.writeln(arguments[i]);
}
})(1,2,3,4);</script>

4.callee属性
callee属性是arguments的一个属性,返回方法的正文。即:func.arguments.callee
= func;

<script language="javascript" type="text/javascript">
(function argTest(a,b,c,d){
alert("函数需要 "+argTest.length+" 个参数");
alert("已经传入的参数为:"+arguments.length+"个");
document.writeln("参数分别为:");
for(var i=0;i<arguments.length;i++){
document.writeln(arguments[i]);
}
alert(arguments.callee); //增加此句,将alert显示出整段代码
})(1,2,3,4);</script>

利用callee属性可以轻松实现递归调用:

function fact(n){
if(n<=0){
return 1;
}else{
return n*arguments.callee(n-1); //轻松实现递归调用
}
}
alert(fact(3));
5.caller属性
caller属性是方法的一个属性,返回当前调用该方法的方法。
如果在A方法中调用了B方法,在A方法执行的过程中,在B函数中存在B.caller等于A的方法体。
说明
对于函数来说,caller 属性只有在函数执行时才有定义。假如函数是由顶层调用的,那么 caller 包含的就是 null
。假如在字符串上下文中使用 caller 属性,那么结果和 functionName.toString
相同,也就是说,显示的是函数的反编译文本。
下面的例子说明了 caller 属性的用法:

function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handleCaller() {
callerDemo();
}
handleCaller(); //返回handleCaller方法体
callerDemo(); //返回this is a top function function one(){two();}
function three(){one();}
function two(){alert(two.caller);} //显示结果为one的方法体
three();

javascript中call、apply、argument、callee、caller的更多相关文章

  1. javascript中call,apply,bind的用法对比分析

    这篇文章主要给大家对比分析了javascript中call,apply,bind三个函数的用法,非常的详细,这里推荐给小伙伴们.   关于call,apply,bind这三个函数的用法,是学习java ...

  2. JavaScript中的apply和call函数详解(转)

    每个JavaScript函数都会有很多附属的(attached)方法,包括toString().call()以及apply().听起来,你是否会感到奇怪,一个函数可能会有属于它自己的方法,但是记住,J ...

  3. JavaScript中call,apply和prototype

    [TOC] call()方法 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 如果没有提供 thi ...

  4. JavaScript中的apply和call函数详解

    本文是翻译Function.apply and Function.call in JavaScript,希望对大家有所帮助 转自“http://www.jb51.net/article/52416.h ...

  5. javascript中的apply,call,bind详解

    apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Jav ...

  6. JavaScript中的apply()和call()

    可以将call()和apply()看做是某个对象的方法,通过调用方法的形式来间接调用函数. call()和apply()的第一个实参是要调用函数的母对象,它是调用上下文,在函数体内通过this来获得对 ...

  7. Javascript中的apply、call、bind

    apply . call .bind 三者都是用来改变函数的this对象的指向的: apply . call .bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文: apply . ...

  8. javascript中bind,apply,call的相同和不同之处

    javasctipt中bind,apply,call的相同点是: 1,都是用来改变this的指向; 2,都可以通过后续参数进行传参; 3,第一个参数都是指定this要指向的对象; 不同点: 1,调用方 ...

  9. JavaScript中的apply,call与this的纠缠

    1.apply定义 apply:调用函数,并用指定对象替换函数的 this 值,同时用指定数组替换函数的参数. 语法:apply([thisObj[,argArray]]) thisObj 可选.要用 ...

  10. Javascript中的apply与call详解

    JavaScript中有一个call和apply方法,其作用基本相同,但也有略微的区别. 一.方法定义 1.call 方法 语法:call([thisObj[,arg1[, arg2[, [,.arg ...

随机推荐

  1. BPMN2.0 规范

    1 启动事件 每个流程总是以启动事件作为入口,启动事件在BPMN2.0 中以细线圆圈表示.分为三种类型 空启动事件 定时启动事件 异常启动事件 消息启动事件 启动事件都是等待第三方触发才可以启动. 定 ...

  2. Windows Azure Mobiles Services实现client的登录注冊

    下文仅仅是简单实现,client以Android端的实现为例: 用户表Account: package com.microsoft.ecodrive.model; public class Accou ...

  3. jdbc连接hive0.14

    Jdbc连接hive0.14版本号 眼下官网最新版本号是hive0.13,要想下载最新的hive得去git上去clone一个. Hive0.14最大特点是支持直接插入. 如今做一个jdbc连接hive ...

  4. [hihoCoder] #1096 : Divided Product

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given two positive integers N and M, please divide N into sev ...

  5. CocoaPods did not set the base configuration of your project 问题解决方式

    今天在使用pod install的时候.出现了 [!] CocoaPods did not set the base configuration of your project because you ...

  6. java concurrency

    Dealing with InterruptedExceptionhttp://www.ibm.com/developerworks/java/library/j-jtp05236/index.htm ...

  7. CoCos2D-X-2.1.5在Eclipse中导入HelloCpp项目搭建

    1.前言 最新正在做一个校园增强现实的应用,虽然不知道cocos2d-x具体到最后能做成什么样子,但还是拿来试试,本文章仅从在Eclipse中采用复制一个新项目副本的方式来导入一个现成的HelloCp ...

  8. ny788 又见Alice and Bob

    又见Alice and Bob 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 集训生活如此乏味,于是Alice和Bob发明了一个新游戏.规则如下:首先,他们得到一个 ...

  9. C++访问mysql数据库

    C++连接mysql数据库,并取数据进行显示本例中,在mysql中已经存在了一个数据库test,并在test数据库中创建了一张表stu做测试,表中包含3个字段 需要把mysql目录下的libmysql ...

  10. ubuntu 文档查看器/gedit查看txt中文乱码问题

    文档查看器界面是中文的,但查看pdf文档只显示英文,中文都空了出来. 用命令: sudo apt-get install poppler-data 解决该问题. gedit查看txt中文乱码问题 打开 ...