Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4549216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:19:44+00:00 2026-05-21T16:19:44+00:00

Had a question about a implementation of bind function that I found on Mozilla’s

  • 0

Had a question about a implementation of bind function that I found on Mozilla’s site. For the most part it makes sense to me, but I cant figure out what this check is for…

this instanceof nop ? this : ( obj || {} ) 

in the bind function. Obviously its checking if ‘this’ is the empty function, but why would you need to bind the empty function. I have tried it in firebug, it works, but what is the point? Just trying to increase my javascript knowledge so any help would be appreciated.

if ( !Function.prototype.bind ) {

  Function.prototype.bind = function( obj ) {

    var slice = [].slice,
    args = slice.call(arguments, 1), 
    self = this, 
    nop = function () {}, 
    bound = function () {
      return self.apply( this instanceof nop ? this : ( obj || {} ), 
                          args.concat( slice.call(arguments) ) );    
    };

    nop.prototype = self.prototype;

    bound.prototype = new nop();

    return bound;
  };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:19:46+00:00Added an answer on May 21, 2026 at 4:19 pm

    Its allows you to call the bound function as a constructor without being bound to the original object. In other words the “bound” function will still work just like the original, unbound version if you call it with new.

    Here’s an example:

    var obj = {};
    
    function foo(x) {
        this.answer = x;
    }
    var bar = foo.bind(obj);   // "always" use obj for "this"
    
    bar(42);
    console.log(obj.answer);   // 42
    
    var other = new bar(1);    // Call bar as a constructor
    console.log(obj.answer);   // Still 42
    console.log(other.answer); // 1
    

    How it works

    To simplify the explanation, here’s a simplified version of the code that only binds this and doesn’t handle arguments or a missing obj parameter:

    Function.prototype.bind = function( obj ) {
      var self = this,
      nop = function () {},
      bound = function () {
        return self.apply( this instanceof nop ? this : obj, arguments );
      };
    
      nop.prototype = self.prototype;
      bound.prototype = new nop();
    
      return bound;
    };
    

    The function that gets returned by Function.prototype.bind behaves differently depending on whether you use it as a function, or a constructor (see Section 15.3.4.5.1 and 15.3.4.5.2 of the ECMAScript 5 Language Specification). The primary difference, is that it ignores the “bound this” parameter when it’s called as a constructor (since inside a constructor, this needs to be the newly-created object). So the bound function needs a way to determine how it’s being called. For example, bound(123) vs. new bound(123) and set this accordingly.

    That’s where the nop function comes in. It’s essentially acting as an intermediate “class” so that bound extends nop which extends self (which is the function bind() was called on). That part is set up here:

    nop.prototype = self.prototype;
    bound.prototype = new nop();
    

    When you call the bound function, it returns this expression:

    self.apply( this instanceof nop ? this : obj, arguments ) )
    

    this instanceof nop works by following the prototype chain to determine the if any prototype of this is equal to nop.prototype. By setting nop.prototype = self.prototype and bound.prototype = new nop(), any object created with new bound() will be created with the original prototype from self via bound.prototype. So inside the function call, this instanceof nop (i.e. Object.getPrototypeOf(nop) == nop.prototype) is true and self gets called with this (the newly created object).

    In a normal function call, ‘bound()’ (without new), this instanceof nop would be false, so obj gets passed as the this context, which is what you would expect on a bound function.

    The reason for using the intermediate function is to avoid calling the original function (in the line bound.prototype = new nop();), which may have side effects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i had asked a question about refering objects in a function that is inside
I had posted another question earlier about deployment with Passenger. That problem turned out
i have a conceptual question about Front controller implementation in php. Most of the
I'm playing around with RhinoCommons and NHibernate and I had a question about the
Some of the answers to a question I had about redirect_to made me think
This is a follow-up to a previous question I had about interfaces. I received
I had a similar question here about allocating and initializing one pointer to struct
one question about NDBCLUSTER. I inherited the writing of a web site basing on
Today i was asked this question about sharing data from a thread t1 that
Another question about my implementation. I am fairly new to java/android coming from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.