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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:07:21+00:00 2026-06-17T16:07:21+00:00

When using constructor function invocation in JavaScript, I have read that the this variable/keyword

  • 0

When using constructor function invocation in JavaScript, I have read that the this variable/keyword within the function will be bound to the new object and will return a object that has all the bound “this” values.

For example:

function Person() {
  this.name = "bob";
  this.age = 33;
}

var person = new Person(); // person object with name property of "bob" and age property of 33

If I do something like this, the same result will occur:

function Person() {
  var localVar = "test",
      fake = "fake";
  this.name = "bob";
  this.age = 33;
}

    var person = new Person(); // person object with name property of "bob" and age property of 33 regardless of the local variables declared inside

However, if I do something like this, the variables/properties declared on self will return in the object

function Person() {
  var self = this;
  self.phone = "123-123-1222";
  self.career = "programmer";
  this.name = "bob";
  this.age = 33;
}

    var person = new Person(); // person object with name property of "bob" and age property of 33, phone of "123-123-1222" and career of "programmer"

In the last example, how does the interpreter know to return all four properties, even though two of them are bound to a local variable of this and not the actual “this” keyword.

  • 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-17T16:07:21+00:00Added an answer on June 17, 2026 at 4:07 pm

    In your last example, self is just a reference to this. They’re interchangeable because they point to the same object:

    function foo() {
        var self = this;
        var that = self;
    
        console.log(self === this);  // true
        console.log(that === this);  // true
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
What's the advantage of using a constructor function like so: var MyLibrary = new
I have this code first the constructor and button click event: using System.Text; using
I have been trying to write a constructor function that would make an object
today I've read we have a way of declaring the function by Function constructor
If I were to make a new function using the Function constructor, how could
Suppose I have a constructor: function Constructor(input) { this.input = input } Constructor.prototype.method =
'Javascript: The Good Parts' provides this example regarding the Constructor Invocation Pattern (page 29-30).
Say I have a nice domain model, using (constructor) DI where needed. Now I
I don't want constructor called. I am using placement new. I just want to

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.