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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:18:38+00:00 2026-06-11T05:18:38+00:00

I am trying to figure out how I can add methods to a constructor

  • 0

I am trying to figure out how I can add methods to a constructor after I have created it.
In my code below, I cannot use Person’s prototype property to add a new public method which has access to Person’s vars. (Do the functions attached to the prototype property not close over the vars in the main function).
Unlike the first way, the second way works – Person 2. seems like these are called privileged methods –http://www.crockford.com/javascript/private.html.

function Person(name, age){}
Person.prototype.details = function(){ 
    return "name: "+name+", age: "+age;
};

function Person2(name, age){
 this.details = function(){ 
    return "name: "+name+", age: "+age;};
}

var per1 = new Person("jim", 22);
var per2 = new Person2("jack", 28);

per1.details();
//=> ReferenceError: age is not defined
per2.details();
//=> "name: jack, age: 28"
  • 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-11T05:18:39+00:00Added an answer on June 11, 2026 at 5:18 am

    Of course not, the function was declared in a scope, different to the scope where the arguments/variables were declared, so JS wouldn’t know which variables you’re on about. Suppose you had a second closure, or better (well, worse actually) yet: a global variable called name. Which one would JS pick?

    Here’s an example for you:

    function MyObject(name)
    {
        var localVar = 'foobar';
        this.evilMethod = (function(localVar)
        {
            return function()
            {
                console.log('localVar = '+localVar);//=== name
            };
        })(name);
        this.badMethod = function()
        {
            console.log('localVar = '+ localVar);// === 'foobar'
        };
    }
    var name = 'Global Name';
    var anotherClosure = (function(name)
    {
        var localVar = name.toLowerCase();
        return function()
        {
            console.log(name);
            console.log(localVar);
        }
    })('Bobby');
    MyObject.prototype.closureVars = function()
    {
        console.log(name);//Global name
        console.log(localVar);//undefined
    };
    

    Now first off: this is terrible code, but you get the point: you can have hundreds of variables with the same name, which one JS has to use, might not always be clear.

    Giving prototypes access to instance closure variables has other implications, too: you could, for instance change their values, which defeats the point of having a closure in the first place.
    But the biggest problem by a country mile would be: multiple instances! If you create a constructor, odds are you’re going to instantiate more than 1 object with it. How would that work, if they all share the same prototype?

    Just assign the arguments/variables you want to access in the prototype to a property, like FishBasketGordo’s example does

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

Sidebar

Related Questions

I'm trying to figure out why I can't I use super in a generic
I am trying to figure out how i can add an object to a
I am trying to figure out how can I update the petevents table with
Trying to figure out how I can do this properly. The print_r looks like
I trying to figure out how I can delete from multiple tables in SQL
I'm trying to figure out how I can listen to the Cancel button that
I am trying to figure out how I can tell, in MATLAB, how many
I'm trying to figure out how I can download a particular tag of a
I am trying to figure out how I can update my sql query dynamically.
I am trying to figure out how I can define an autocmd that influences

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.