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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:27:01+00:00 2026-05-31T05:27:01+00:00

What is the difference between function person(first_name, last_name) { this.first = first_name this.last =

  • 0

What is the difference between

function person(first_name, last_name) {
    this.first = first_name
    this.last  = last_name
}

and this:

function person(first_name, last_name) {
    var first = first_name
    var last  = last_name
}

Why only the first one makes person.first & person.last accessible outside the function?

  • 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-31T05:27:01+00:00Added an answer on May 31, 2026 at 5:27 am

    The this keyword within a function is called the invocation context.

    1) If you define your function as a member of an object (a method):

    myObject.someMethod = function() { this.x = 2; };
    

    then the invocation context, this, is the object to which the method is being added, myObject. So after calling myObject.someMethod(); above, myObject.x is then 2. The member x is undefined until you call the method, unless you defined it before.

    2) If you use your function as a constructor with the new keyword, then this refers to the new object that is being created:

    function MyX() { this.x = 3; };
    var myX = new MyX();
    

    You’ll then have property myX.x set to 3.

    Note that I called my constructor MyX(), not myX(). You should call yours Person(), not person(). It’s just a convention, but it is useful to indicate that a function is meant to be used as a constructor.

    3) Finally, if you use this within a function that you call as neither a method nor a constructor, then this refers to the global object (document or, equivalently, window). Note however that if you are using javascript in strict mode (which you should do), this is undefined in such a situation, which means that you basically cannot use this in a function that is not a method or a constructor.


    Your specific question refers to case 2), the constructor. this.x = 3 in the constructor just sets property x of the newly created object. After some object myX is created, you can then access and modify x externally as any other object property using myX.x.

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

Sidebar

Related Questions

Can any one tell me the difference between replace function & translate function of
What is the difference between the following 2 examples of code: (function(){ var myFunc
Can someone tell me the difference between: $(document).ready(function() { }); and: var someVar =
Is there any difference between function MyFunc() { // code... } and var MyFunc
What is the difference between function mythemes_preprocess_node(&$variables) { ... } and function mythemes_preprocess_node(&$vars) {
I was wondering if there are any difference between function __construct() { parent::__construct(); $this->load->
In Fortran, a clear difference exists between function and subroutine: functions return one value,
Is there any difference between a variable declared as static outside any function between
I was looking at Stack Overflow question What is the difference between abstract function
As the title says, whats the difference between MyFunction = function() { } and

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.