
<script>
let person = {
name: 'heool world',
}
let fullName = function () {
return this.name
}
let name = fullName.apply(person)
console.log(name);
let person1 = {
name: 'heool world',
}
let fullName1 = function (age, test) {
console.log(typeof age)
return this.name + age + test
}
// applay 只能传递数组
let name1 = fullName1.apply(person1, [12, 88])
console.log(name1);
// 这种方式是不对的
// let name5 = fullName1.apply(person1,12)
// console.log(name5);
// call 方法传递的参数如果只有一个参数,则传递数组,会当成对象来打印
let name2 = fullName1.call(person1, 12, 88)
console.log(name2);
let name3 = fullName1.call(person1, [12, 88], 33)
console.log(name3);
// call 方法不能传递对象
</script>
apply 和 call 的第一个参数都是对象,第二个参数起是调用函数的参数。注意,是函数调用apply
网友评论