在js中,函数也是对象,所以也有属性和方法
属性
- caller 指向被调用的函数
function outer() {
innert()
}
function inner(){
alert(inner.caller);
}
outer(); //outer, 因为是outer调用了inner
//解决依赖
function innert() {
alert(arguments.callee.caller); argument.callee指向所在函数的名字
}
- this 指向当前函数执行环境对象
- length 指向函数希望参数的长度
function sayName(name) {
}
alert(sayName.length); //1
- prototype 指向函数的原型对象
函数
- apply
- call
都是扩充函数的作用域,区别是传递参数的方式不同
window.color = "red";
var o = {color: "blue"};
function syaColor() {
return this.color;
}
syaColor(this); //red
syaColor(window); //red
syaColor(o); // blue
3.bind 还有一个扩展作用域的方法 , 不同的是返回一个绑定作用域后的函数
window.color = "red";
var o = {color:"blue"};
function syaColor() {
return this.color;
}
var bindFunction = syaColor.bind(o);
bindFunction(); //blue