Call Apply 使用方法 和 区别
一、区别:
区别:在于传参时:Apply是数组 [xxx,xxx]的形式;Call是xxx,xxx的形式 ,下面例子可看
<template>
<div>
<p>Call 参数:{{ fullName3 }}</p>
<p>Apply 参数:{{ fullName4 }}</p>
</div>
</template>
<script>
export default {
name: "XuexiKuangjiaCallApply",
data() {
return {
person: {
firstName: "AAA",
lastName: "BBB",
fullName: function () {
return this.firstName + this.lastName;
},
},
name: {
firstName: "111",
lastName: "222",
},
person2: {
fullName: function () {
return this.firstName + this.lastName;
},
fullName3: function (x, y) {
return this.firstName + this.lastName + x + y;
},
},
};
},
computed: {
fullName2: function () {
return this.person2.fullName.call(this.name);//结果111222
},
fullName3: function () {
return this.person2.fullName3.call(this.name, 5, 6);//结果 11122256
},
fullName4: function () {
return this.person2.fullName3.apply(this.name, [8, 8]);//结果 11122288
},
},
};
</script>
<style lang="scss" scoped>
</style>