call
1 2 3 4 5 6 7 8
| // 函数.call(对象,参数1,参数2...参数N) // 函数.apply(对象,[参数1,参数2...参数N]) function say(n) { console.log('叫'+ n +'声' + this.v); } var miao = {v: 'miaomiao'}; say.call(miao, 1); // 叫1声miaomiao say.apply(miao, [2]); // 叫2声miaomiao
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function say() { return this.v; } var miao = {v: 'miaomiao'}; var dog = {v: 'wangwang'}; var sheep = { v: function() { return 'meimei'; } }; [miao, dog, sheep].map(function (animal) { var s = say.call(animal); var result = Object.prototype.toString.call(s) == "[object Function]" ? s() : s; console.log(result); }); // miaomiao wangwang meimei
|