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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:22:48+00:00 2026-06-15T02:22:48+00:00

I am new to prototype, and I just made a simple example that won’t

  • 0

I am new to prototype, and I just made a simple example that won’t work. From the code below its obvious what I want – a counter. But for some reason the output is always 5, I guess that the bind is making a deep copy of the this object, which is then bound to the function. There is an obvious solution where I make everything public, but I would like to know if there is a more elegant solution. (Or you may correct my and tell me that there is no deep copy, but some other bug). The documentation didn’t help me.

The code:

<!DOCTYPE html>
<html>
    <body>
        <input type="button" value="click" onclick="javascript: doTest();" />
        <div id="canvas"></div>
        <script>
            var ClassA = function (position, element) { // constructor
              this.field1 = position;
              this.target = element;
            };

            ClassA.prototype = function () {
              // private functions
                var _aPrivateMethod = function(){
                    console.log(this);
                    this.field1 += 1;
                    return this.field1;
                };

                var _aPublicMethod = function(){
                    this.target.innerHTML = _aPrivateMethod.bind(this)();
                };

                return { // interface
                    constructor: ClassA,
                    aPublicMethod : _aPublicMethod
                };
            }();

            function doTest() {
                var obj = new ClassA(4, document.getElementById('canvas'));
                obj.aPublicMethod();
            }
        </script>
    </body>
</html>
  • 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-15T02:22:49+00:00Added an answer on June 15, 2026 at 2:22 am

    Here are a number of issues. First you call doTest each time. A new object is created on each click, so there’s no way to increment field1.

    As I understood you want field1 to be private. In the way you initialize it, it cant be private. You can do something like:

    function Person() {
       var counter = 0;
       this.increment = function () {
           return ++counter;
       }
    }
    

    In that way you can’t access the counter from outside the function scope.

    Another issue is that you don’t call the private method with the right context. You should use _aPrivateMethod.call(this) as pimvdb have mentioned.

    Here is an example of:

    function Person() {
    
        var age = 0;
    
        function incrementAge() {
            return ++age;
        }
    
        function eatCake() {
            console.log('Yey. I\'ll eat cake!');
        }
    
        this.birthday = function () {
            eatCake();
            return incrementAge();
        }
    }
    
    var p = new Person();
    p.birthday(); //'Yey. I'll eat cake!', returns 1
    p.birthday(); //'Yey. I'll eat cake!', returns 2
    

    In that way eatCake, incrementAge and age are all private because of the functional scope.
    The bad thing in the example is the way I’ve declared the function birthday. For each instance of Person a new copy will be created.
    The right way is:

    Person.prototype.birthday = function () { ... }
    

    My declaration is not like this one because I want to use a private data members. Actually not private just local for the function (age, eatCake, incrementAge).
    There’s no way to access them from a function which is declared in the prototype, so that’s why for the example above I’ve choose that approach.

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

Sidebar

Related Questions

There is currently this Prototype code that does a PUT: new Ajax.Request(someUrl, { method:
Just wondering how to test that a prototype is available based on its name.
Why does the Cat.prototype = new Mammal() line below not work inside the Cat()
Here's my code: Image.prototype.x = 0; Image.prototype.y = 0; var blankImage = new Image();
Here's a very simple Prototype example. All it does is, on window load, an
I'm pretty new to the whole prototype idea in Javascript, from what I've gathered
I'm working on a new prototype for an application and I'm conducing some tests
I'm a new to Prototype and I can't really understand the minimalist documentation provided
I am new to prototype and finding it a lot more difficult than jquery.
could anyone please tell me, where in Javascript the difference between MyClass.prototype = new

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.