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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:54:40+00:00 2026-05-14T19:54:40+00:00

Normally, I’ve seen prototype functions declared outside the class definition, like this: function Container(param)

  • 0

Normally, I’ve seen prototype functions declared outside the class definition, like this:

function Container(param) {
    this.member = param;
}
Container.prototype.stamp = function (string) {
    return this.member + string;
}

var container1 = new Container('A');
alert(container1.member);
alert(container1.stamp('X'));

This code produces two alerts with the values “A” and “AX”.

I’d like to define the prototype function INSIDE of the class definition. Is there anything wrong with doing something like this?

function Container(param) {
    this.member = param;
    if (!Container.prototype.stamp) {
        Container.prototype.stamp = function() {
            return this.member + string;
        }
    }
}

I was trying this so that I could access a private variable in the class. But I’ve discovered that if my prototype function references a private var, the value of the private var is always the value that was used when the prototype function was INITIALLY created, not the value in the object instance:

Container = function(param) {
    this.member = param;
    var privateVar = param;
    if (!Container.prototype.stamp) {
        Container.prototype.stamp = function(string) {
            return privateVar + this.member + string;
        }
    }
}
var container1 = new Container('A');
var container2 = new Container('B');
alert(container1.stamp('X'));
alert(container2.stamp('X'));

This code produces two alerts with the values “AAX” and “ABX”. I was hoping the output would be “AAX” and “BBX”. I’m curious why this doesn’t work, and if there is some other pattern that I could use instead.

EDIT: Note that I fully understand that for this simple example it would be best to just use a closure like this.stamp = function() {} and not use prototype at all. That’s how I would do it too. But I was experimenting with using prototype to learn more about it and would like to know a few things:

  • When does it make sense to use prototype functions instead of closures? I’ve only needed to use them to extend existing objects, like Date. I’ve read that closures are faster.
  • If I need to use a prototype function for some reason, is it “OK” to define it INSIDE the class, like in my example, or should it be defined outside?
  • I’d like to understand why the privateVar value of each instance is not accessible to the prototype function, only the first instance’s value.
  • 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-14T19:54:41+00:00Added an answer on May 14, 2026 at 7:54 pm

    When does it make sense to use prototype functions instead of closures?

    Well, it’s the most lightweight way to go, let’s say you have a method in the prototype of certain constructor, and you create 1000 object instances, all those objects will have your method in their prototype chain, and all of them will refer to only one function object.

    If you initialize that method inside the constructor, e.g. (this.method = function () {};), all of your 1000 object instances will have a function object as own property.

    If I need to use a prototype function for some reason, is it “OK” to define it INSIDE the class, like in my example, or should it be defined outside?

    Defining the members of a constructor’s prototype inside itself, doesn’t makes much sense, I’ll explain you more about it and why your code doesn’t works.

    I’d like to understand why the privateVar value of each instance is not accessible to the prototype function, only the first instance’s value.

    Let’s see your code:

    var Container = function(param) {
        this.member = param;
        var privateVar = param;
        if (!Container.prototype.stamp) {  // <-- executed on the first call only
            Container.prototype.stamp = function(string) {
                return privateVar + this.member + string;
            }
        }
    }
    

    The key point about your code behavior is that the Container.prototype.stamp function is created on the first method invocation.

    At the moment you create a function object, it stores the current enclosing scope in an internal property called [[Scope]].

    This scope is later augmented when you call the function, by the identifiers (variables) declared within it using var or a FunctionDeclaration.

    A list of [[Scope]] properties forms the scope chain, and when you access an identifier (like your privateVar variable), those objects are examined.

    And since your function was created on the first method invocation (new Container('A')), privateVar is bound to the Scope of this first function call, and it will remain bound to it no matter how do you call the method.

    Give a look to this answer, the first part is about the with statement, but in the second part I talk about how the scope chain works for functions.

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

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want a white space separated argument treated as… May 15, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer The problem is that you've declared a brand new type,… May 15, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer Maybe you release the delegate in dealloc of BMSStringPickerController? May 15, 2026 at 7:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.