面向对象的JavaScript-006-Function.prototype.bind() 的4种作用

1.

  1 // Function.prototype.bind() 的作用
  2
  3     // 1.Creating a bound function
  4     this.x = 9;
  5     var module = {
  6       x: 81,
  7       getX: function() { return this.x; }
  8     };
  9
 10     console.log(module.getX()); // 81
 11
 12     var retrieveX = module.getX;
 13     console.log(retrieveX());
 14     // 9, because in this case, "this" refers
 15     // to the global object
 16
 17     // Create a new function with ‘this‘ bound to module
 18     // New programmers might confuse the
 19     // global var x with module‘s property x
 20     var boundGetX = retrieveX.bind(module);
 21     console.log(boundGetX()); // 81
 22
 23     // 2.Partially applied functions
 24
 25     // The next simplest use of bind() is to make a function with pre-specified initial arguments. These arguments (if any) follow the provided this value and are then inserted at the start of the arguments passed to the target function, followed by the arguments passed to the bound function, whenever the bound function is called.
 26     function list() {
 27       return Array.prototype.slice.call(arguments);
 28     }
 29
 30     var list1 = list(1, 2, 3); // [1, 2, 3]
 31     console.log(list1);
 32     // Create a function with a preset leading argument
 33     var leadingThirtysevenList = list.bind(undefined, 37);
 34     console.log(leadingThirtysevenList);
 35     var list2 = leadingThirtysevenList();
 36     // [37]
 37     console.log(list2);
 38     var list3 = leadingThirtysevenList(1, 2, 3);
 39     // [37, 1, 2, 3]
 40     console.log(list3);
 41
 42     // 3.With setTimeout
 43
 44     //y default within window.setTimeout(), the this keyword will be set to the window (or global) object. When working with class methods that require this to refer to class instances, you may explicitly bind this to the callback function, in order to maintain the instance.
 45     function LateBloomer() {
 46       this.petalCount = Math.ceil(Math.random() * 12) + 1;
 47     }
 48
 49     // Declare bloom after a delay of 1 second
 50     LateBloomer.prototype.bloom = function() {
 51       window.setTimeout(this.declare.bind(this), 1000);
 52     };
 53
 54     LateBloomer.prototype.declare = function() {
 55       console.log(‘I am a beautiful flower with ‘ +
 56         this.petalCount + ‘ petals!‘);
 57     };
 58
 59     var flower = new LateBloomer();
 60     flower.bloom();
 61     // after 1 second, triggers the ‘declare‘ method
 62
 63     // 3.Bound functions used as constructors
 64     // Bound functions are automatically suitable for use with the new operator to construct new instances created by the target function. When a bound function is used to construct a value, the provided this is ignored. However, provided arguments are still prepended to the constructor call:
 65     function Point(x, y) {
 66       this.x = x;
 67       this.y = y;
 68     }
 69
 70     Point.prototype.toString = function() {
 71       return this.x + ‘,‘ + this.y;
 72     };
 73
 74     var p = new Point(1, 2);
 75     p.toString(); // ‘1,2‘
 76
 77     // not supported in the polyfill below,
 78
 79     // works fine with native bind:
 80
 81     var YAxisPoint = Point.bind(null, 0/*x*/);
 82
 83
 84     var emptyObj = {};
 85     var YAxisPoint = Point.bind(emptyObj, 0/*x*/);
 86
 87     var axisPoint = new YAxisPoint(5);
 88     axisPoint.toString(); // ‘0,5‘
 89
 90     console.log(axisPoint instanceof Point); // true
 91     console.log(axisPoint instanceof YAxisPoint); // true
 92     console.log(new Point(17, 42) instanceof YAxisPoint); // true
 93
 94     // Example can be run directly in your JavaScript console
 95     // ...continuing from above
 96
 97     // Can still be called as a normal function
 98     // (although usually this is undesired)
 99     console.log(YAxisPoint(13));
100
101     console.log(emptyObj.x + ‘,‘ + emptyObj.y);
102     // >  ‘0,13‘
103
104     // 4.Creating shortcuts
105     var slice = Array.prototype.slice;
106
107     // ...
108
109     slice.apply(arguments);
110     // same as "slice" in the previous example
111     var unboundSlice = Array.prototype.slice;
112     var slice = Function.prototype.apply.bind(unboundSlice);
113
114     // ...
115
116     slice(arguments);
117
118     // Polyfill
119     // The bind function is an addition to ECMA-262, 5th edition; as such it may not be present in all browsers. You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of bind() in implementations that do not natively support it.
120     if (!Function.prototype.bind) {
121       Function.prototype.bind = function(oThis) {
122         if (typeof this !== ‘function‘) {
123           // closest thing possible to the ECMAScript 5
124           // internal IsCallable function
125           throw new TypeError(‘Function.prototype.bind - what is trying to be bound is not callable‘);
126         }
127
128         var aArgs   = Array.prototype.slice.call(arguments, 1),
129             fToBind = this,
130             fNOP    = function() {},
131             fBound  = function() {
132               return fToBind.apply(this instanceof fNOP
133                      ? this
134                      : oThis,
135                      aArgs.concat(Array.prototype.slice.call(arguments)));
136             };
137
138         if (this.prototype) {
139           // Function.prototype doesn‘t have a prototype property
140           fNOP.prototype = this.prototype;
141         }
142         fBound.prototype = new fNOP();
143
144         return fBound;
145       };
146     }

时间: 2024-10-17 05:40:28

面向对象的JavaScript-006-Function.prototype.bind() 的4种作用的相关文章

理解 JavaScript 中的 Function.prototype.bind

函数绑定(Function binding)很有可能是你在开始使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其实就是 Function.prototype.bind(),只是你有可能仍然没有意识到这点. 第一次遇到这个问题的时候,你可能倾向于将this设置到一个变量上,这样你可以在改变了上下文之后继续引用到它.很多人选择使用 self, _this 或者 context 作为变量名称(也有人使用 that)

《javascript设计模式与开放实践》学习(一)Function.prototype.bind

使用Function.prototype.bind来包装func函数 1.简化版的bind Function.prototype.bind=function (context) { var self=this; //保存原函数 return function () { return self.apply(context,arguments); } }; var obj={name:'seven'}; var func=function(){ alert(this.name); }.bind(ob

浅析 JavaScript 中的 Function.prototype.bind() 方法

Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个对象,bind() 方法会创建一个函数,函数体内this对象的值会被绑定到传入bind() 函数的值. 例如,在 f() 函数上调用 bind() 方法并传入参数 obj ,即 f.bind(obj) ,这将返回一个新函数, 新函数会把原始的函数 f() 当做 obj 的方法来调用,就像 obj.f() 似的,当然这时 f() 函数中的 this 对象指向的是 obj . 简单使用情形一 va

Function.prototype.bind接口浅析

本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind Function.prototype.bind Syntax fun.bind(thisArg[, arg1[, arg2[, ...]]]) Parameters thisArg The value to be passed as the thi

一个简易版的Function.prototype.bind实现

重新看<JavaScript设计模式与开发实践>一书,第32页发现个简易版的Function.prototype.bind实现,非常容易理解,记录在这了. Function.prototype.bind = function (context) { var self = this; return function () { return self.apply(context, arguments); }; }; var obj = { name: 'sven' }; var func = fu

Polyfill Function.prototype.bind的四个阶段

昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实它就是用来静态绑定函数执行上下文的this属性,并且不随函数的调用方式而变化. 示例: ? 1 2 3 4 5 6 7 8 9 test('Function.prototype.bind', function(){    function orig(){      return this.x;   

Function.prototype.bind

解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. bind的作用 bind最直接的作用就是改变this的指向 // 定义函数 var checkNumericRange = function (value) { if (typeof value !== 'number') return false; else return value >= this

浅析function.prototype.bind

作用: 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. bind最直接的作用就是改变this的指向: // 定义函数 var checkNumericRange = function (value) { if (typeof value !== 'number') return false; else return value >= this.minimum && value <= th

Function.prototype.bind 简介

bind可以解决两种问题: 1. 可以改变一个函数的 this 指向 2. 可以实现偏函数等高阶功能 本文暂且讨论第一个功能 USE CASE var foo = { x: 3 } var bar = function(){ console.log(this.x); } bar(); // undefined var boundFunc = bar.bind(foo); boundFunc(); // 3 简易版实现方式 Function.prototype.bind = function (s