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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:13:03+00:00 2026-05-18T10:13:03+00:00

I find myself needing: var self = this; a lot within my javascript ‘classes’.

  • 0

I find myself needing:

var self = this;

a lot within my javascript ‘classes’. Although this is commonly done, it feels a bit wrong.
What I’m hoping to find in this question is a better way to deal with this, or a something to convince me this is quite alright.

Is this the standard way to keep the correct bindings around? Should I standardize on using ‘self’ everywhere, unless i explicitly need ‘this’.

edit: I know exactly why I need this, I’m just wondering if it’s considered a bit evil and why. I’m aware there’s also the ‘apply’ built-in javascript function to explicitly define scope when calling a method. Is it better?

  • 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-18T10:13:04+00:00Added an answer on May 18, 2026 at 10:13 am

    As others have said: This “extra variable” is (at some level) the only way to get about the fact that this is a special expression and thus, being not a variable, is not bound in an execution context/closure.

    However, what I think you are asking (or what I really want to answer) is:

    Should one put var self = this at the top of every method/constructor?

    Summary

    While I tried this once, and had the same question, I no longer use this approach. Now I reserve the construct for when I need access in a closure. To me it adds a little “hey, this is what I really want!” semantic to my code:

    this -> this and self -> this (but really that) in a closure

    Questions ala carte:

    …Although this is commonly done, it feels a bit wrong. What I’m hoping to find in this question is a better way to deal with this, or a something to convince me this is quite alright.

    Do what feels right to you. Don’t be afraid to try one method and switch back later (but please try to remain consistent within each project 🙂

    Is this the standard way to keep the correct bindings around? Should I standardize on using ‘self’ everywhere, unless i explicitly need ‘this’.

    “self” is the most common name used. As per above, I prefer the opposite approach — to use this except when a closure binding is required.

    ..if it’s considered a bit evil and why.

    Evil is a silly subjective term (albeit fun sometimes). I’ve never said it was evil, just why I do not follow the approach. Some people tell me I am “evil” for not using semi-colons. I tell them they should actually come up with good arguments and/or learn JavaScript better 🙂

    I’m aware there’s also the ‘apply’ built-in javascript function to explicitly define scope when calling a method. Is it better?

    The problem with apply/call is that you must use them at point of the function invocation. It won’t help if someone else calls one of your methods as the this may already be off. It’s most useful for doing things like the jQuery-style callbacks where the this is the element/item of the callback, etc.

    As an aside…

    I like to avoid “needing self” on members and thus generally promote all member functions to properties where the receiver (this) just “flows through”, which is normally “as expected”.

    The “private” methods in my code begin with a “_” and if the user calls them, that’s on them. This also works better (is required, really) when using the prototype approach to object creation. However, Douglas Crockford disagrees with this “private” approach of mine and there are some cases where the look-up chain may thwart you by injecting an unexpected receiver:

    Using the “self” bound in the constructor also locks the upper limit of the look-up chain for a method (it is no longer polymorphic upward!) which may or may not be correct. I think it’s normally incorrect.

    Happy coding.

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

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
I commonly find myself extracting common behavior out of classes into helper/utility classes that
I find myself using this logic a lot: if (isset($array[$key])) $array[$key] += $num; else
I find myself needing to store public key certificates, and a single private key
I find myself needing to generate a checksum for a string of data, for
I find myself defining classes like: struct AngleSize { explicit AngleSize(double radians) : size(radians)
I frequently find myself writing code like this: List<int> list = new List<int> {
I find myself always needing to thin out objects before sending them over the
I find myself writing this class often in my python code when I need
I find myself doing a lot of: (concat coll [e]) where coll is 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.