特:
函数实际参数:Arguments对象
callee属性:引用当前正在执行函数;
1、属性length
arguments数组的length属性指定了传递给该函数的实际参数数目;
函数length属性返回该函数形式参数数目;只读属性;
function check(args){
var actual = args.length;
var expected = args.callee.length;
if (actual != expected) {
throw new Error ("Wrong number of arguments: expected: "+
expected + ";actually passed " + actuall;
}
}
function f(x, y, z){
//检查实际参数个数与期望的参数个数是否匹配
check(arguments);
return x + y + z;
}
2、 属性prototype
引用预定义的原型对象;原型对象在使用new运算符把函数作为构造函数时起作用。
3、方法apply和call
特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域
1)call()和apply()第一个参数都是调用的函数的对象,在函数体内这一参数是关键字this的值。
call()和apply()的剩余参数是传递给要调用的函数的值
function add(c,d){
return this.a + this.b + c + d;
}
var s = {a:1, b:2};
console.log(add.call(s,3,4)); // 1+2+3+4 = 10
console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14
2)实现继承
function Animal(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
function Cat(name){
Animal.call(this, name);
}
var cat = new Cat("Black Cat");
cat.showName();