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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:56:34+00:00 2026-06-09T08:56:34+00:00

I am looking for var self = this alternative plan. var Animal = function(name){

  • 0

I am looking for var self = this alternative plan.

var Animal = function(name){
  this.name = name;
  this.arr = [1,2,3,4];
  this.inc = function(num){
      return num + 1;
  };

  this.fireArr = function(){
    var self = this;
    this.arr.forEach(function(item){
      console.log(self.inc(item));
    });

  };

};

var dog = new Animal("dog");
console.log(dog.fireArr());

My fiddle is here.

http://jsfiddle.net/haradashinya/TtYpc/

Do you have any idea?

Thanks in advance.

  • 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-09T08:56:35+00:00Added an answer on June 9, 2026 at 8:56 am

    You can use .bind() to make sure the function is called with the right this value:

    function fireArr() {
        this.arr.forEach(function(item){
            console.log(this.inc(item));
        }.bind(this));
    }
    

    But imho the self (that, _this) variable is easier to understand, because it directly states that not the normal this value is used, although one would expect it (e.g. in an event handler, or jQuery’s each()). Especially on long functions, where you don’t see the bind() in the end, this is of importance. Also, some ancient browsers do not support bind() and you would need to shim it.

    So, for any in-place function expressions I recommend the use of a dereferencing variable.

    But it can be of great use when you have a method defined somewhere, normally using this to point to the current object as it is common in that context, and then the method should be used somewhere else. Instead of a var self-wrapper, you can and should use bind for simplicity and clarity. Your example offers quite a good demo (assuming the inc method used the this keyword):

    this.arr.forEach( this.inc.bind(this) );
    

    (although forEach() allows us to pass a custom this argument – event attachers for example don’t)

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

Sidebar

Related Questions

I'm looking at this code snippet: var addSnippet = function( req, res ) {
Possible Duplicate: JS: var self = this? When looking at arbitrary code written in
I have a map array with objects stuffed with variables looking like this: var
I'm looking for a way to do something like this: var db = new
I'm looking to geoencode an address and convert it to GPS Coordinates. Basically, var
Any advice how to access an internal method var of jQuery Masony? I'm looking
Given the following registrations builder.Register<A>().As<I>(); builder.Register<B>().As<I>(); builder.Register<C>().As<I>(); var container = builder.Build(); I am looking
Looking to do a bit of refactoring... Using NHibernate I have this query currently
I am creating my jQuery element like this: var addLink = $(<a/>, { click:
Ok, so I thought I understood this (no pun intended), but apparently not. var

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.