bar.bind.bind

功能绑定 (Function Bind)

bind is a method on the prototype of all functions in JavaScript. It allows you to create a new function from an existing function, change the new function’s this context, and provide any arguments you want the new function to be called with. The arguments provided to bind will precede any arguments that are passed to the new function when it is called.

bind是JavaScript所有函数原型的一种方法。 它允许您从现有函数创建新函数,更改新函数的this上下文,并提供希望与新函数一起调用的参数。 提供给bind的参数将在调用新函数时传递给新函数的所有参数之前。

使用bind在函数中更改this设置 (Using bind to change this in a function)

The first argument provided to bind is the this context the function will be bound to. If you do not want to change the value of this pass null as the first argument.

提供给第一个参数bindthis上下文功能将被绑定到。 如果您不想更改this传递的null ,则将null作为第一个参数。

You are tasked with writing code to update the number of attendees as they arrive at a conference. You create a simple webpage that has a button that, when clicked, increments the numOfAttendees property on the confrence object. You use jQuery to add a click handler to your button, but after clicking the button the confrence object has not changed. Your code might look something like this.

您的任务是编写代码,以在与会者到达会议时更新他们的人数。 您创建一个简单的网页,有一个按钮,点击后,增量numOfAttendees的confrence对象的属性。 您使用jQuery向按钮添加了单击处理程序,但是单击按钮后confrence对象没有更改。 您的代码可能看起来像这样。

var nodevember = {numOfAttendees: 0,incrementNumOfAttendees: function() {this.numOfAttendees++;}// other properties
};$('.add-attendee-btn').on('click', nodevember.incrementNumOfAttendees);

This is a common problem when working with jQuery and JavaScript. When you click the button the this keyword in the method you passed to jQuery’s on method references the button and not the conference object. You can bind the this context of your method to solve the problem.

在使用jQuery和JavaScript时,这是一个常见问题。 当您单击按钮时,传递给jQuery的on方法的方法中的this关键字引用该按钮,而不是会议对象。 您可以绑定方法的this上下文来解决问题。

var nodevember = {numOfAttendees: 0,incrementNumOfAttendees: function() {this.numOfAttendees++;}// other properties
};$('.add-attendee-btn').on('click', nodevember.incrementNumOfAttendees.bind(nodevember));

Now when the button is clicked this references the nodevember object.

现在,当单击按钮时, this引用了nodevember对象。

使用bind为函数提供参数 (Providing arguments to a function with bind)

Each argument passed to bind after the first will precede any arguments passed when the function is called. This allows you to pre-apply arguments to a function. In the example below, combineStrings takes two strings and concatenates them together. bind is then used to create a function that always provides “Cool” as the first string.

在第一个参数之后传递给bind每个参数将在调用函数时传递的任何参数之前。 这使您可以将参数预先应用于函数。 在下面的示例中, combineStrings接受两个字符串并将它们串联在一起。 然后, bind用于创建始终提供“ Cool”作为第一个字符串的函数。

function combineStrings(str1, str2) {return str1 + " " + str2
}var makeCool = combineStrings.bind(null, "Cool");makeCool("trick"); // "Cool trick"

The guide on this reference has more information about how what the this keyword references can change.

此参考指南提供了有关this关键字参考的更改方式的更多信息。

More details on the bind method can be found on Mozilla’s MDN docs.

有关bind方法的更多详细信息,请参见Mozilla的MDN文档 。

功能长度 (Function Length)

The length property on the function object holds the number of arguments expected by the function when called.

函数对象上的length属性保存调用时函数期望的参数数量。

function noArgs() { }function oneArg(a) { }console.log(noArgs.length); // 0console.log(oneArg.length); // 1

ES2015语法 (ES2015 Syntax)

ES2015, or ES6 as it is commonly called, introduced the rest operator and default function parameters. Both of these additions change the way the length property works.

ES2015或俗称的ES6引入了rest运算符和默认函数参数。 这两个添加都更改了length属性的工作方式。

If either the rest operator or default parameters are used in a function declaration the length property will only include the number of arguments before a rest operator or a default parameter.

如果在函数声明中使用了rest运算符或默认参数,则length属性将仅在rest运算符或默认参数之前包含参数数量。

function withRest(...args) { }function withArgsAndRest(a, b, ...args) { }function withDefaults(a, b = 'I am the default') { }console.log(withRest.length); // 0console.log(withArgsAndRest.length); // 2console.log(withDefaults.length); // 1

More Information on Function.length can be found on Mozilla’s MDN Docs.

可以在Mozilla的MDN文档中找到有关Function.length更多信息。

翻译自: https://www.freecodecamp.org/news/function-prototype-bind-and-function-prototype-length-in-javascript-explained/

bar.bind.bind

bar.bind.bind_JavaScript中的function.prototype.bind和function.prototype.length解释相关推荐

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

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

  2. Function.prototype.bind相关知识点

    1 var addNum = { // 创建一个方法,给val的值 加num 2 num: 5, 3 fun: function(val) { 4 return this.num + val; 5 } ...

  3. 2.cocos2dx 3.2中语法的不同之处,lambada表达式的使用和function和bind函数的使用

    1        打开建好的T32  Cocos2dx-3.2的一个项目 2        设置Cocos显示窗口的位置是在AppDelegate.cpp中: 3  设置自适应窗口大小的代码是在上面的 ...

  4. 【Boost】boost库中function和bind一起使用的技巧(一)

    1 bind/function 引 (1)头文件 bind函数#include <boost/bind.hpp> function使用头文件#include <boost/funct ...

  5. 聊聊Function的bind()

    bind顾名思义,绑定. bind()方法会创建一个新函数,当这个新函数被调用时,它的this值是传递给bind()的第一个参数,它的参数是bind()的其他参数和其原本的参数. 上面这个定义最后一句 ...

  6. JavaScript中的call,apply,bind学习总结

    JavaScript 中的 call, apply, bind 这三个函数的作用和区别在各大论坛都被讨论了很多次了,但是我一直都还没来得及好好总结,这次正好看到了一个很不错的关于JavaScript ...

  7. [C++]C++11:Function与Bind

    std::function 它是函数.函数对象.函数指针.和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针. 以统一的方式处理函数.函数对象.函数指针.和成员函数. ...

  8. 传递function_boost库function与bind

    boost库function与bind 一.function 头文件:boost/function.hpp function更合适的说法我觉得是一种回调函数的表现方式. boost::function ...

  9. C++11 std::function, std::bind, std::ref, std::cref

    C++11 std::function, std::bind, std::ref, std::cref 转自:http://www.jellythink.com/ std::function 看看这段 ...

最新文章

  1. 【原创】vmware tools点了安装却安装不了的问题解决方法
  2. 廖雪峰Java1-2Java程序基础-3整数运算
  3. OpenCV准密集立体声Quasi dense Stereo
  4. 使用 Log4Net 记录日志
  5. Ribbon、Feign和OpenFeign的区别来了
  6. Django和SQLAlchemy,哪个Python ORM更好?
  7. deap软件结果分析_绿港科技之窗:基于InDel标记的黄瓜种质资源遗传多样性分析...
  8. window下Ionic环境安装
  9. 【Landsat 8】介绍
  10. 使用python3编写冒险岛079登录器
  11. win10超好看的鼠标主题,你也来试试吧
  12. java实现 GB35114 sip AuthorizationHeader遇到的一个小坑
  13. 女程序员写代码被骂,把领导萌死,成功治愈“选择困难症”
  14. 第14期《学海无涯乐作舟》12月刊
  15. Window API 第五篇 WTSEnumerateProcesses
  16. python的xlwt模块_Python xlwt模块
  17. 边缘化(marginalization )和稀疏化(sparsification)---ceres-solver
  18. MFC CFile输出文本文件乱码
  19. 【原创】登录和注册页推荐 尊重原创 记得点赞
  20. 多线程在单核cpu与多核cpu下如何工作

热门文章

  1. word表格美化技巧:如何统一改变表格的样式2+续表制作
  2. cygwin 安装php5.6,Cygwin视窗下的UNIX多面手
  3. 社恐人必备逃跑神器-模拟来电+模拟短信+模拟钱包+模拟关机
  4. 系统架构设计师考试题库笔记重点6:信息化基础
  5. 工业机器人码垛教学实施_工业机器人课程丨看似简单的工业机器人码垛,你会操作吗?...
  6. 涨幅1800%!一图了解84家5G上市企业2020净利润预告情况
  7. AVAGO MegaRAID SAS 9361-8i配置IPMI以及JBOD直通模式
  8. 应用计算机测定pn结,应用计算机测定PN结正向压降的温度特性
  9. php生成svg文字图形,SVG基础|绘制SVG文字
  10. 【ROM定制】Android 12 制作『MIUI官改』那点事⑤去除