1.

 // Function.prototype.bind() 的作用

     // 1.Creating a bound function
this.x = 9;
var module = {
x: 81,
getX: function() { return this.x; }
}; console.log(module.getX()); // var retrieveX = module.getX;
console.log(retrieveX());
// 9, because in this case, "this" refers
// to the global object // Create a new function with 'this' bound to module
// New programmers might confuse the
// global var x with module's property x
var boundGetX = retrieveX.bind(module);
console.log(boundGetX()); // // 2.Partially applied functions // 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.
function list() {
return Array.prototype.slice.call(arguments);
} var list1 = list(1, 2, 3); // [1, 2, 3]
console.log(list1);
// Create a function with a preset leading argument
var leadingThirtysevenList = list.bind(undefined, 37);
console.log(leadingThirtysevenList);
var list2 = leadingThirtysevenList();
// [37]
console.log(list2);
var list3 = leadingThirtysevenList(1, 2, 3);
// [37, 1, 2, 3]
console.log(list3); // 3.With setTimeout //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.
function LateBloomer() {
this.petalCount = Math.ceil(Math.random() * 12) + 1;
} // Declare bloom after a delay of 1 second
LateBloomer.prototype.bloom = function() {
window.setTimeout(this.declare.bind(this), 1000);
}; LateBloomer.prototype.declare = function() {
console.log('I am a beautiful flower with ' +
this.petalCount + ' petals!');
}; var flower = new LateBloomer();
flower.bloom();
// after 1 second, triggers the 'declare' method // 3.Bound functions used as constructors
// 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:
function Point(x, y) {
this.x = x;
this.y = y;
} Point.prototype.toString = function() {
return this.x + ',' + this.y;
}; var p = new Point(1, 2);
p.toString(); // '1,2' // not supported in the polyfill below, // works fine with native bind: var YAxisPoint = Point.bind(null, 0/*x*/); var emptyObj = {};
var YAxisPoint = Point.bind(emptyObj, 0/*x*/); var axisPoint = new YAxisPoint(5);
axisPoint.toString(); // '0,5' console.log(axisPoint instanceof Point); // true
console.log(axisPoint instanceof YAxisPoint); // true
console.log(new Point(17, 42) instanceof YAxisPoint); // true // Example can be run directly in your JavaScript console
// ...continuing from above // Can still be called as a normal function
// (although usually this is undesired)
console.log(YAxisPoint(13)); console.log(emptyObj.x + ',' + emptyObj.y);
// > '0,13' // 4.Creating shortcuts
var slice = Array.prototype.slice; // ... slice.apply(arguments);
// same as "slice" in the previous example
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.apply.bind(unboundSlice); // ... slice(arguments); // Polyfill
// 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.
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
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 aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
}; if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP(); return fBound;
};
}

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

  1. 理解javascript中的Function.prototype.bind

    在初学Javascript时,我们也许不需要担心函数绑定的问题,但是当我们需要在另一个函数中保持上下文对象this时,就会遇到相应的问题了,我见过很多人处理这种问题都是先将this赋值给一个变量(比如 ...

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

    Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个对象,bind() 方法会创建一个函数,函数体内this对象的值会被绑定到传入bind() 函数 ...

  3. 理解 JavaScript 中的 Function.prototype.bind

    函数绑定(Function binding)很有可能是你在开始使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其 ...

  4. JavaScript 函数绑定 Function.prototype.bind

    ECMAScript Edition5 IE9+支持原生,作用为将一个对象的方法绑定到另一个对象上执行. Function.prototype.bind = Function.prototype.bi ...

  5. javascript Function.prototype.bind

    语法: fn.bind(obj,arg1,arg2,arg3...) bind是es5新增的方法,顾名思义,它的作用是将函数绑定到某个对象上,就像是某个对象调用方法一样.其本质还是改变了该函数的上下文 ...

  6. JavaScript 中的 Function.prototype.bind() 方法

    转载自:https://www.cnblogs.com/zztt/p/4122352.html Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个 ...

  7. javascript 一些函数的实现 Function.prototype.bind, Array.prototype.map

    * Function.prototype.bind Function.prototype.bind = function() { var self = this, context = [].shift ...

  8. 一起Polyfill系列:Function.prototype.bind的四个阶段

    昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...

  9. Function.prototype.bind接口浅析

    本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...

随机推荐

  1. PhpStorm 2017.3 版本在 Mac 系统 macOS High Sierra 版本 10.13.3 中运行很卡顿

    最近升级了系统,发现PHPStorm 运行一会儿就卡顿起来了,按网上的方法加大内存配置也是没效果: 运行一会儿照样卡顿,接着一会儿就要内存溢出了挂掉了: 想着最近只有升级过操作系统,并没有升级JDK等 ...

  2. CentOS6.8编译安装LAMP

    CentOS6.8编译安装Apache2.4.25.MySQL5.7.16.PHP5.6.29 初始化 #固定IP vi /etc/sysconfig/network-scripts/ifcfg-et ...

  3. PHP使用RabbitMQ

    基本概念 Broker:简单来说就是消息队列服务器实体. Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列. Queue:消息队列载体,每个消息都会被投入到一个或多个队列. Bind ...

  4. GOF23设计模式之桥接模式(bridge)

    一.桥接模式概述 桥接模式核心要点: 处理多层继承结构,处理多维度变化的场景,将各个维度设计成独立的继承结构,使各个维度可以独立的扩展在抽象层建立关联. 二.桥接模式场景提出与存在问题 商城系统中常见 ...

  5. appium+python自动化33-解锁九宫格(TouchAction)

    TouchAction 1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.py class TouchActio ...

  6. java代码---------再练习ChatAt()的用法

    总结: 没有理解方法的含义.瞎用 package com.mmm; //实现字符串中某个字符出现的次数 public class Mo { public static void main(String ...

  7. Java中的三元运算:a = (a > b)?a:b

    格式:逻辑值 ? 表达式1 : 表达式2 执行顺序:先执行逻辑值,如果逻辑值为true,则执行表达式1:反之则执行表达式2 a = (a > b)?a:b 如果a>b成立,返回a: 如果a ...

  8. 安装FreePBX的ISO版本

    下载地址:http://schmoozecom.com/distro-download.php 这个相当于系统了,第一步:安装程序会提示选择你想安装Asterisk的版本:现在出现了11版本,这个根据 ...

  9. View.findViewById()和Activity.findViewById()区别

    在网上看见View.findViewById() 和 Activity.findViewById()执行效率不一样 使用Activity.findViewById()如: TextView tv_in ...

  10. html2pdf 中文支持问题

    系统用的是HTML2PDF V4.0.3 版本 百度后 http://blog.sina.com.cn/s/blog_6b0ce0310101fdv6.html 发现中文支持不好 还是有乱码问题 解决 ...