JavaScript in function bind ()

Source: Internet
Author: User

Function bind () and currying

<%--

All JavaScript functions has a method called bind, 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 bound is passed as the height of the 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 is 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 was bound to null, i.e. the global object, and the x and yarguments were bound to 1 and 2 respect Ively. 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 had arguments that we pass into those functions. But how does JavaScript handles what do you ' re passing in are not always clear. When you start the getting into object-oriented development and you are may find yourself perplexed over so you have access to value s sometimes but not the other times.

When passing in a primitive type variable like a string or a number, the value is passed in by value. This means, changes to, variable while in the function is completely separate from anything that happens outs IDE 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 a object, however, passes it in by reference. In this case, the 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 do you pass in a method of an object? Most would expect (or at least I do) that it would being passed by reference allowing the method to access other parts of th E 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 is the "the use of the" this keyword. It's a handy short-hand for referring and the current object context. When passing a function as a parameter, though, the context is lost. More accurately, now refers to the context of the object making the call instead of the this object ' s function we just PA Ssed in. For standalone functions, this would is the window object and for functions called from a event, this would is the event O Bject.

Solving the problem

There is possible ways to get around this.

Option 1:when You know the method

If you know the method of the object that would be called then it's fairly easy. Just pass in the object instead of the function and call that instead. Using the From the last objectchanger 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 and the need to pass both the method and the object as Paramet ERs and use the call method. call is part of the JavaScript specification and allows a function to run in the context of another object. As a result, the this keyword would reference the right Object:the object we passed in.

Here's our function one more time objectchanger :

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, 2006CATEGORIZED as Javascriptshort URL:https://snook.ca/s/503

JavaScript in function bind ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.