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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:01:48+00:00 2026-06-18T14:01:48+00:00

When defining a JS object with a constructor function, is there any way to

  • 0

When defining a JS object with a constructor function, is there any way to avoid having to use “this” with every single object property? It seems very ugly. E.g.

function Thingy(abc) {
  this.var1 = abc;
  this.var2 = this.var1 + " hello ";
  // etc
}

var myObj = new Thingy();

It seems that I should be able to use “var var2” and then leave out the “this” and just refer to “var2”, but am I missing something?

  • 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-18T14:01:49+00:00Added an answer on June 18, 2026 at 2:01 pm

    Well, I’m afraid that you’re running into how the language is designed.

    But there is a sense in which you can use the plain var statements from the constructor. Any functions created in there have access to the closure that includes these properties:

    function Thingy(abc) {
        var var1 = abc;
        this.func1 = function(str) {return var1 + ", hello";};
    }
    
    var thing1 = new Thingy("Dan");
    thing1.func1();  // => "Dan, hello"
    

    Note this is how you can encapsulate entirely private variables in an object, so it’s often a useful technique.

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

Sidebar

Related Questions

Are there any specific benefits derived from defining a utility function directly on the
I am defining an object like this: function Project(Attributes, ProjectWidth, ProjectHeight) { this.ProjectHeight =
Currently have this code defining getter/setter for an object's selectedID property. The object is
It seems there are different ways the __repr__ function can return. I have a
Whats the difference between defining the method area as a property of this instead
I'm re-defining a method in an object in ruby and I need the new
Defining infinite list in Haskell: [1,1..] => [1,1,1,..] Or, the circular way: lst=1:lst Is
Defining custom components in Facelets is easy and quick but there's one thing I
Defining a form in my Play! controller when the compiler spit out this weird
Suppose I am defining a surface object class surface { private: vector<point> points_; vector<hexFace>

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.