From 659553be7b16832950bad5a6fca3c0cda3ba0c39 Mon Sep 17 00:00:00 2001 From: Songhn Date: Thu, 30 Sep 2021 22:42:20 +0800 Subject: Update Web/JavaScript/Reference/Global_Objects/Function/bind, zh-CN (#2601) - remove polyfill - added polyfill link --- .../global_objects/function/bind/index.html | 71 +--------------------- 1 file changed, 1 insertion(+), 70 deletions(-) (limited to 'files') diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html index e979676aa3..c612abb1df 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/function/bind/index.html @@ -212,76 +212,6 @@ var slice = Function.prototype.apply.bind(unboundSlice); slice(arguments); -

Polyfill

- -

有两种实现bind的方法,下面第一种不支持使用new调用新创建的构造函数,而第二种支持。

- -
// Does not work with `new (funcA.bind(thisArg, args))`
-if (!Function.prototype.bind) (function(){
-  var slice = Array.prototype.slice;
-  Function.prototype.bind = function() {
-    var thatFunc = this, thatArg = arguments[0];
-    var args = slice.call(arguments, 1);
-    if (typeof thatFunc !== 'function') {
-      // closest thing possible to the ECMAScript 5
-      // internal IsCallable function
-      throw new TypeError('Function.prototype.bind - ' +
-             'what is trying to be bound is not callable');
-    }
-    return function(){
-      var funcArgs = args.concat(slice.call(arguments))
-      return thatFunc.apply(thatArg, funcArgs);
-    };
-  };
-})();
- -

你可以将这段代码插入到你的脚本开头,从而使你的 bind() 在没有内置实现支持的环境中也可以部分地使用bind

- -
//  Yes, it does work with `new (funcA.bind(thisArg, args))`
-if (!Function.prototype.bind) (function(){
-  var ArrayPrototypeSlice = Array.prototype.slice;
-  Function.prototype.bind = function(otherThis) {
-    if (typeof this !== 'function') {
-      // closest thing possible to the ECMAScript 5
-      // internal IsCallable function
-      throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
-    }
-
-    var baseArgs= ArrayPrototypeSlice.call(arguments, 1),
-        baseArgsLength = baseArgs.length,
-        fToBind = this,
-        fNOP    = function() {},
-        fBound  = function() {
-          baseArgs.length = baseArgsLength; // reset to default base arguments
-          baseArgs.push.apply(baseArgs, arguments);
-          return fToBind.apply(
-                 fNOP.prototype.isPrototypeOf(this) ? this : otherThis, baseArgs
-          );
-        };
-
-    if (this.prototype) {
-      // Function.prototype doesn't have a prototype property
-      fNOP.prototype = this.prototype;
-    }
-    fBound.prototype = new fNOP();
-
-    return fBound;
-  };
-})();
- -

上述算法和实际的实现算法还有许多其他的不同 (尽管可能还有其他不同之处,但已经没有必要再多列举):

- - - -

如果你选择使用这部分实现,你不能依赖于那些与 ECMA-262, 5th edition 规定的行为偏离的例子。bind() 函数被广泛支持之前,某些情况下(或者为了兼容某些特定需求对其做一些特定修改后)可以选择用这个实现作为过渡。

- -

请访问 https://github.com/Raynos/function-bind 以查找更完整的解决方案!

-

规范

@@ -318,6 +248,7 @@ if (!Function.prototype.bind) (function(){

相关链接