call()是一个对象可以继承、使用另一个对象的函数的方法,是一种拿来主义的办法。这个要和this来配合使用。
var xyj = {
name: '孙悟空',
age: 1000,
gender: '男',
jieshao: function() {
return "姓名:" + this.name + "\n年龄:" + this.age + "\n性别:" + this.gender;
},
jieshao2: function(loc) {
return loc + ":\n" + "姓名:" + this.name + "\n年龄:" + this.age + "\n性别:" + this.gender;
}
};
var xyj2 = {
name: '猪八戒',
age: 800,
gender: '男',
}
var intro = xyj.jieshao.call(xyj2);
console.log(intro);
var intro2 = xyj.jieshao2.call(xyj2, '高老庄');
console.log(intro2);