Skip to content

Instantly share code, notes, and snippets.

@jerrylau91
Created August 25, 2017 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerrylau91/dbfde864fe40cede49728fd9f9e66cab to your computer and use it in GitHub Desktop.
Save jerrylau91/dbfde864fe40cede49728fd9f9e66cab to your computer and use it in GitHub Desktop.
JS Function.prototype.bind usage
## What does Function.prototype.bind do ?
Function.prototype.bind = function (scope) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var fn = this;
return function () {
return fn.apply(scope);
};
}
@jerrylau91
Copy link
Author

JavaScript 中的函数原型链中有一个 bind 方法,可以用来静态绑定函数的上下文。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment