var Person = { name : ‘alice‘, say : function(txt1,txt2) { console.info(txt1+txt2); console.info(this.name); }} var Dog = { name : ‘tom‘, say : function(txt1,txt2) { console.info(txt1+txt2); console.info(this.name); }}var arr = [‘hello‘,‘hi‘];Person.say(‘hello‘,‘hi‘);Dog.say(‘wang~‘,‘wang2~‘);Person.say.call(Dog,‘hello‘,‘hi‘);//Person.say内部的this指向了Dog,多个参数用逗号隔开Person.say.apply(Dog,arr);//第二个参数是数组,参数数量可以是未知的var PersonSay = Person.say.bind(Dog,‘hello‘,‘hi‘);//不会立即执行,触发返回函数才会执行PersonSay(); >>>hellohi>>>alice>>>wang~wang2~>>>tom>>>hellohi>>>tom>>>hellohi>>>tom>>>hellohi>>>tom
原文地址:https://www.cnblogs.com/Alice-Xu/p/8798503.html
时间: 2024-10-13 22:36:51