JavaScript中apply、call、callee常见方法区分
使用arguments.callee方法实现匿名函数递归操作
(function(num){
  num++;
  if (num<10) {
    console.log('num='+num);
    //使用arguments.callee方法实现匿名函数递归
    arguments.callee(num);
  };
})(4)
补充  ES5 提示: 在严格模式下"use strict",arguments.callee 会报错 TypeError,因为它已经被废除了。 
apply与call方法
两者都是应用某一对象的一个方法,用另一个对象替换当前对象,区别是
apply() 方法在指定 this 值和参数(参数以数组或 类数组对象 的形式存在)的情况下调用某个函数.
call() 方法在使用一个指定的 this 值和若干个指定的参数值的前提下调用某个函数或方法.
student 对象实现对person对象继承
//Person
function Person(name, age){
    this.name = name;
    this.age = age;
}
//Student
function Student(name, age, grade){
    this.grade = grade;
    //应用某一对象的一个方法,用另一个对象替换当前对象
    //student扩展(或继承)Person对象name、age方法
    //Person.apply(this, arguments);
    Person.call(this, name, age)
}
var person = new Person('Avensatr', '24');
// person====>>Person {name: "Avensatr", age: "24"}
 
var student = new Student('Avensatr', '24', '高级中学');
// student====>>Student {name: "Avensatr", age: "24", grade: "高级中学"}
apply与call 计算
function caculate(c, d){
    var result = this.a+this.b+c+d;
    return result;
}
var obj = {
    a : 5,
    b : 15
}
var c = 10;
var d = 15;
//apply,call方法使得  obj对象被别人用
var num1 = caculate.apply(obj, [c, d]);
var num2 = caculate.call(obj, c, d);
资料参考
https://developer.mozilla.org/en-US/
http://www.jb51.net/onlineread/JavaScript-Garden-CN/#function.arguments

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号