var p1 = {
name:'小明',
age:'12',
action:function(where,doing){
console.log(this.age + '岁的'+this.name + '在' + where + doing);
}
}
var p2 = {
name:'小红',
age:'15'
}
console.log(p1.action.call(p2,'操场上','运动')) var p1 = {
name:'小明',
age:'12',
action:function(where,doing){
console.log(this.age + '岁的'+this.name + '在' + where + doing);
}
}
var p2 = {
name:'小红',
age:'15'
}
console.log(p1.action.call(p2,'操场上','运动')) 12岁的小明在undefined undefined
12岁的小明在操场上运动
15岁的小红在undefined undefined
15岁的小红在操场上运动
let obj = {
name: 'zs',
age: 19,
};
function foo(a, b) {
console.log(this);
console.log(a, b);
};
foo.call(obj, 5, 6);//5 6
foo.apply(obj, [7, 8]);//7 8
foo.bind(obj)(2, 3); //不会立即调用 赋值的时候调用//2 3
三者第一个值均为对象 apply 后两个数为[ ]数组形式 15岁的小红在操场上运动
我第一次写这么多可能有不足希望指正
var p1 = {
name:'小明',
age:'12',
action:function(where,doing){
console.log(this.age + '岁的'+this.name + '在' + where + doing);
}
}
var p2 = {
name:'小红',
age:'15'
}
console.log(p1.action.call(p2,'操场上','运动'))