JavaScript/call bind apply:修订间差异
来自康健生活
创建页面,内容为“== 引言 == == call == === 上下文 === === 继承 ===” |
|||
| 第3行: | 第3行: | ||
=== 上下文 === | === 上下文 === | ||
=== 继承 === | === 继承 === | ||
== bind == | |||
=== bind 实现 === | |||
<syntaxhighlight lang="javascript"> | |||
Function.prototype.bind = function(newThis) { | |||
var aArgs = Array.prototype.slice.call(arguments, 1) //拿到除了newThis之外的预置参数序列 | |||
var that = this | |||
return function() { | |||
return that.apply(newThis, aArgs.concat(Array.prototype.slice.call(arguments))) | |||
//绑定this同时将调用时传递的序列和预置序列进行合并 | |||
} | |||
}</syntaxhighlight> | |||