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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:19:16+00:00 2026-06-10T20:19:16+00:00

I have this code: function Person(name){ var self = this; this.name = name; function

  • 0

I have this code:

function Person(name){      
    var self = this;

    this.name = name;

    function hello(){
        alert("hello " + self.name);
    }

    return {
        hello: hello
    };
}

var newPerson = new Person("john");

newPerson.hello();

I want to be able to use the ‘this’ keyword to access the ‘name’ property in the ‘hello’ function; I want an alternative to using the ‘self’ variable.

Except using the $.proxy function of jquery to control the context, how can I write the same code but without the variable ‘self’?

I want a code that looks like below but ‘name’ is always ‘undefined’ when I call ‘newPerson.hello()’. I don’t know why because I have always believed that the scope of a function is always the object at the left of the dot of the caller and in this case, it’s ‘newPerson’ that have been assign a value ‘john’ when creating the object.

function Person(name){              
    this.name = name;

    function hello(){
        alert("hello " + this.name);
    }

    return {
        hello: hello
    };
}

var newPerson = new Person("john");

newPerson.hello();

Thank you.

  • 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-10T20:19:17+00:00Added an answer on June 10, 2026 at 8:19 pm

    Don’t use return by default when using the new keyword the function will return this
    you will need to change how your function is declared how ever.

    Here is a fiddle

    http://jsfiddle.net/SaintGerbil/9SAhD/

    function Person(name){              
        this.name = name;
        this.hello = function (){
            alert("hello " + this.name);
        }
    }
    
    var newPerson = new Person("john");
    
    newPerson.hello();​
    

    EDIT if you require name to be private then here is an alternative

    function Person(name){              
        var _name = name;
        this.hello = function (){
            alert("hello " + _name);
        }
    }
    
    var newPerson = new Person("john");
    
    newPerson.hello();​
    

    In answer to your question there are 4 ways to call a function
    These affect the value of this they are

    • Constructor call (using the new keyword) where this is the new object which is returned automatically.
    • Method call if a function is attached to an object and called from it then this is the object called from.
    • Function call a direct call to a function will result in this being the global object, (a common mistake when calling a contstructor without new)
    • Apply\Call call when using a method to specify what this should be (see jackwanders as an example)

    FURTHER EDIT

    So taking your desired code at the start and explaining what is happening.

    function Person(name){
        this.name = name; // 1
    
        function hello(){ // 2
            alert("hello " + this.name); // 3
        } 
    
        return {
            hello: hello
        }; //4
    }
    

    Person as a function can be called two ways:

    var x = Person("ted");

    and

    var x = new Person("jimmy");

    Since you have named Person with a capital it implies that you are expecting people to use new.
    So sticking with that we enter the function and javascript creates a new object and assigns it to this.

    • line 1 we then attach a ‘name’ variable to this and initialise with the passed parameter.
    • line 2 we then attach a ‘hello’ function to this.
    • line 3 the function expects to have a ‘name’ variable exist attached to this (which it does for now).
    • line 4 rather than return this (default behavior) we are now declaring a new object and attaching the function to it. This new object does not have a ‘name’ variable with in its scope.

    So when you create the object you get an object with a function attached but it cannot get to the variable it needs to execute correctly.
    Which is why you are getting the undefined.

    Does that make sense I always worry that I’m waffling when I have to expand the textbox?

    Or if I wrote out your function as verbosely as possible it would look something like this.

    function Person(name){
      var this = new Object();
      this.name = name;
      var hello = function (){
        alert("hello " + this.name);
      } 
      this.hello = hello;
    
      var that = new Object();
      that.hello = hello;
    
      return that;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code here: var Person = (function() { var name; var PersonConstructor
i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div
I have this code: var Person = { name: , changes: { name to:
I have the following code: function Person(){ this.age = 30; } function Stats(){ this.age
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
I have this code: _trackit: function(){ for(var key in this.items.sublinks){ switch(key){ case 'shoes': for(var
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have this piece of code: function func1(text) { var pattern = /([\s\S]*?)(\<\?(?:attrib |if
I have found this example on StackOverflow: var people = new List<Person> { new
I have this code: function Keyboard() { this.log = $('#log')[0]; this.pressedKeys = []; this.bindUpKeys

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.