博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 中 function bind()
阅读量:5134 次
发布时间:2019-06-13

本文共 4824 字,大约阅读时间需要 16 分钟。

Function bind() and currying

<%--

All JavaScript functions have a method called bind that binds to an object and returns a new function. The first argument to bind sets the this context of the function.

function area (height) {     return this.width * height; }var obj = { width : 5 };var bound = area.bind(obj);alert(bound(4));             // => 20

  

Calling bound(4); invokes the original function area as a method of obj, like obj.area(4);. The argument you pass tobound is passed as the height argument to the function area.

In addition to binding a function to an object,--%>

EcmaScript 5 supports a bind method that brings native currying to JavaScript. You no longer need to use a curry helper function. The arbitrary number of arguments that you pass to bind are also bound.

function add(x, y, z) {    return x + y + z;}var partial = add.bind(null, 1, 2);       var result = partial(3);              // pass 3 for the z argument alert(result);                        // => 6

  

This creates a new function called partial. The this value is bound to null, i.e. the global object, and the x and yarguments are bound to 1 and 2 respectively. Calling partial with the argument value 3 binds this value to z and then executes the add function without the need to write a curry function.

-------------------------------------------------------------------------------

JavaScript: Passing by Value or by Reference

In JavaScript, we have functions and we have arguments that we pass into those functions. But how JavaScript handles what you’re passing in is not always clear. When you start getting into object-oriented development, you may find yourself perplexed over why you have access to values sometimes but not other times.

When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are completely separate from anything that happens outside the function. Let’s take a look at the following example:

function myfunction(x){      // x is equal to 4      x = 5;      // x is now equal to 5}var x = 4;alert(x); // x is equal to 4myfunction(x); alert(x); // x is still equal to 4

Passing in an object, however, passes it in by reference. In this case, any property of that object is accessible within the function. Let’s take a look at another example:

function myobject(){	this.value = 5;}var o = new myobject();alert(o.value); // o.value = 5function objectchanger(fnc){	fnc.value = 6;}objectchanger(o);alert(o.value); // o.value is now equal to 6

So, what happens when you pass in a method of an object? Most would expect (or at least I did) that it would be passed by reference allowing the method to access other parts of the object it is apart of. Unfortunately, that’s not the case. Check out this example:

function myobject(){	this.value = 5;}myobject.prototype.add = function(){	this.value++;}var o = new myobject();alert(o.value); // o.value = 5o.add();alert(o.value); // o.value = 6function objectchanger(fnc){	fnc(); // runs the function being passed in}objectchanger(o.add);alert(o.value); // sorry, still just 6

The problem here is the use of the ‘this’ keyword. It’s a handy short-hand for referring to the current object context. When passing a function as a parameter, though, the context is lost. More accurately, this now refers to the context of the object making the call instead of the object’s function we just passed in. For standalone functions, this would be the window object and for functions called from an event, this would be the event object.

Solving the problem

There are two possible ways to get around this.

Option 1: When you know the method

If you know the method of the object that will be called then it’s fairly easy. Just pass in the object instead of the function and call that instead. Using theobjectchanger from the last example you’d get the following:

function objectchanger(obj){	obj.add(); // runs the method of the object being passed in}objectchanger(o);alert(o.value); // the value is now 7

Option 2: When you don’t know the method

If you don’t know the method of the object being passed in then you need to pass both the method and the object as parameters and use the call method. callis part of the JavaScript specification and allows a function to run in the context of another object. As a result, the this keyword will reference the right object: the object we passed in.

Here’s our objectchanger function one more time:

function objectchanger(fnc, obj){	fnc.call(obj); // runs the method of the object being passed in}objectchanger(o.add, o);alert(o.value); // the value is now 7

Happy Scripting!

PUBLISHED JANUARY 18, 2006 · UPDATED SEPTEMBER 14, 2006
CATEGORIZED AS 
SHORT URL: 
https://snook.ca/s/503

转载于:https://www.cnblogs.com/oxspirt/p/5906445.html

你可能感兴趣的文章
类间关系总结
查看>>
properties配置文件读写,追加
查看>>
Linux环境下MySql安装和常见问题的解决
查看>>
lrzsz——一款好用的文件互传工具
查看>>
ZPL语言完成条形码的打印
查看>>
这20件事千万不要对自己做!
查看>>
Linux环境下Redis安装和常见问题的解决
查看>>
玩转小程序之文件读写
查看>>
HashPump用法
查看>>
cuda基础
查看>>
virutalenv一次行安装多个requirements里的文件
查看>>
Vue安装准备工作
查看>>
.NET 母版页 讲解
查看>>
Android Bitmap 和 Canvas详解
查看>>
最大权闭合子图
查看>>
oracle 创建暂时表
查看>>
201421410014蒋佳奇
查看>>
导入导出数据库和导入导出数据库表
查看>>
linux下操作mysql
查看>>
【03月04日】A股滚动市盈率PE历史新低排名
查看>>