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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:58:18+00:00 2026-05-22T02:58:18+00:00

I have this piece of code: var Human=function(name){ this._name=name; }; Human.prototype.Shout=function(){ alert(this._name); }; var

  • 0

I have this piece of code:

var Human=function(name){
  this._name=name;
};
Human.prototype.Shout=function(){
  alert(this._name);
};

var tom=new Human("tom");
var john=new Human("john");
alert(tom.Shout===john.Shout);

Right now ._name is not “private”. I want to make ._name “private”, but at the same time i do not wish to create additional functions for each instance of Human (in other words tom.Shout Must be === to john.Shout) because creating additional functions for each instance is just well.. unnecessary (ok offtopic – we can debate this on another thread)

My conclusion is that what I’m trying to achieve (having ._name be “private” and at the same time having tom.Shout===john.Shout) is impossible.

But I just want to be 200% sure before jumping into any conclusions.

(I welcome any hacks as long as the requirements are met, i.e no creating of additional functions for each instance)

If we have to create additional functions to do scoping that’s fine but that number should be a fixed number and that number should not increase with each additional instance of Human.

  • 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-22T02:58:19+00:00Added an answer on May 22, 2026 at 2:58 am

    Update

    Your looking for @name which is an instance variable. Pray it’s in es.next, but we don’t have it yet. Maybe in two years.

    If you care about a clean API then here is your solution:

    function Class(foo) {
        Class.priv(this).foo = foo;
    }
    
    Class.priv = (function() {
        var cache = [],
            uid = 1;
    
        return function(obj) {
            if (!this.__id) {
                this.__id = uid;
                cache[uid++] = {};
            }
            return cache[this.__id];
        };
    
    }());
    
    Class.prototype.bar = function() {
        console.log(Class.priv(this).foo);
    }
    

    Store all the data in a cache as a function of the constructor. No data is cluttered on the object.

    Original

    However there is no such thing as “private”.

    All you can do is create a local variable inside a function.

    The constructor function

    var Human = function(name) {
        // local variable.
        var _name = name;
    }
    

    Has a local variable that by very definition of being local is not usable outside of the constructor function.

    This means that you cannot access it in external code like the prototype.

    What you can do however is make it read only using ES5

    var Human = function(name) {
        Object.defineProperty(this, "name", { value: name });
    }
    

    If you can truly achieve what your asking, you’d make a huge breakthrough in js. I’ve attempted to do just that for many hours.

    A different pattern would be :

    var Human = function(name) {
       this.name = name;
    
       return {
           Shout: this.Shout.bind(this)
       };
    }
    
    Human.prototype.Shout = function() {
       console.log(this.name);
    }
    

    This has the overhead of calling .bind and creating a new object for every instance though.

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

Sidebar

Related Questions

I have this piece of code: this.json.each(function(obj, index) { var li = new Element('li');
I have this piece of code , geocoder = new GClientGeocoder(); var state; function
I have this piece of code: var myObj = function () { this.complex =
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have this piece of code: function func1(text) { var pattern = /([\s\S]*?)(\<\?(?:attrib |if
I have this piece of code: $(document).on(click, #breadNavMain, function() { for(var i = 0;
I have this piece of code: function CallAPI(paramString) { var returnVal; var jqxhr =
OK, I have this piece of code, var record = new Object(); //var aData
I have this piece of code var f = function() { this.x = 100;
I have this piece of code in javascript: var catId = $.getURLParam(category); In my

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.