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

  • Home
  • SEARCH
  • 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 8315995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:15:24+00:00 2026-06-08T21:15:24+00:00

It’s common to see CommonJS modules defined using the following idiom: (function() { var

  • 0

It’s common to see CommonJS modules defined using the following idiom:

(function() {
   var logThis = function() { console.log(this); }
   module.exports = logThis;
}).call(this);

Underscore.js, for example, does this.

I just spend half an hour discussing with a colleague why they invoke the closure with call(this). This will cause the value of this inside the closure to be inherited from the caller, rather than being set to the global object. However, when I tested this in Node.js, the value of this inside the module was always the global object, even when I loaded and ran it like this:

var bar = {};
bar.foo = function() { var foo = require("./foo"); foo(); }

I was really expecting to see the bar object in the console, but actually I see the global object. It then occurred to me that this might be because modules like Underscore.js are also used in a web context. But in that case it would be loaded with a <script> tag so this will always be equal to the global object anyway.

What gives? I’m sure there is a reason for using this construct but I can’t see the practical difference in this particular case whether the module is being used in Node.js or in a webpage.

Update: Just to clarify, I can think of a number of cases where this could make a difference. For example, if I say:

var bar = {}
var foo = require("./foo");
bar.foo = foo;
bar.foo();

(Thanks to @Pointy for correcting my original example.)

I would expect the closure in the module to be evaluated when require() is called, which means that the value of this inside it would be bound to the global object, which would be written to the console even though foo() is then invoked as a member of the “bar” object. However, I am seeing the “bar” object in the console even in this example. I guess that this is not being bound to the closure as I expected?

In a nutshell, I’m looking for one example where a module like Underscore.js will have different behavior due to being wrapped in a closure invoked with fn.call(this) instead of just fn(), either in Node.js or in a web page.

  • 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-06-08T21:15:25+00:00Added an answer on June 8, 2026 at 9:15 pm

    Your call to “foo” inside “bar.foo” is made without any context, so the global context is used. The fact that it’s inside a function where this refers to “bar” is not relevant; that’s just not how JavaScript works. The only thing that matters is how the function is invoked, not where it’s invoked, in other words.

    If “bar.foo” looked like this:

    bar.foo = function() { require("./foo"); foo.call(this); }
    

    then you’d see “bar” in the console. Or, you could do this:

    var bar = {};
    require("./foo");
    bar.foo = foo; 
    

    Then calling bar.foo() would also log the “bar” object. (Does that really work in Node? That is, I thought require() returned an object, and that it didn’t just leave things in the global scope. I’m a rank novice at Node however.)

    edit — OK thanks for updating. Thus, my example would be corrected as follows. First, I think that your module should look like this:

    (function() {
       var logThis = function() { console.log(this); }
       module.exports.logThis = logThis;
    }).call(this);
    

    That is, I think that you want to explort the “logThis” function, so it needs to be bound to the “exports” object as a named property.

    Then:

    var bar = {};
    var foo = require("./foo");
    // At this point, foo.logThis is the function 
    bar.foo = foo.logThis;
    // Now the "foo" property of "bar" is a reference to the same function
    bar.foo(); // logs the "bar" object
    
    var fee = { fie: foo.logThis };
    fee.fie(); // logs the "fee" object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build

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.