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 7782511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:34:03+00:00 2026-06-01T19:34:03+00:00

Trying to wrap my mind around prototypes in Javascript, specifically Node.js, with a simple

  • 0

Trying to wrap my mind around prototypes in Javascript, specifically Node.js, with a simple test.

function Lint() {
    this.input = 'foo';
    events.EventEmitter.call(this);
}

Lint.prototype.dirs = function (dirs) {
    _.each(dirs, this.files);
}

Lint.prototype.files = function (dir) {
    console.log(this.input); // trying to get 'foo', returns undefined
}

var lint = new Lint();

lint.dirs(['js', 'js/views']);

Lint.prototype.files logs undefined because this isn’t referring to the instance of Lint. What am I missing here?

The only solution I can think of, that works, is passing around the initial this from Lint.prototype.dirs to each other function. I’m pretty sure there’s a better way.

  • 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-01T19:34:05+00:00Added an answer on June 1, 2026 at 7:34 pm

    this refers to the lint object, but you’re not passing the Lint object to _.each. You’re detaching the function from the object and passing that.

    You can bind the context of a function to the desired value using Function.prototype.bind…

    _.each(dirs, this.files.bind(this));
    

    Or, you could keep a reference to the this value, and pass an anonymous function to _.each…

    var this_lint = this;
    
    _.each(dirs, function(v) {
        this_lint.files(v);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to wrap my mind around how to do this. For what
I'm trying to wrap my mind around the MVP pattern used in a C#/Winforms
I have spent the afternoon trying to wrap my mind around how to translate
Trying to wrap my head around the jQuery .not() function, and running into a
I'm trying to wrap my mind around MEF. There is one thing I don't
I'm trying to wrap my mind around css3's varying styles for different screen widths
I am still trying to wrap my mind around the workings of OAuth/OpenID, as
I'm trying to wrap my mind around the MVC framework of Sencha Touch, but
I am currently trying to wrap my mind around Java EE 5. What I'd
I'm trying to wrap my mind around F# so I thought it would be

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.