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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:16:04+00:00 2026-05-28T07:16:04+00:00

I read crockford’s page on private members http://javascript.crockford.com/private.html in Javascript and got a question

  • 0

I read crockford’s page on private members http://javascript.crockford.com/private.html in Javascript and got a question which maybe somewhat related. Why should a developer use Prototype?

For example,

For example, I can do this

var Foo = new Object();
Foo.bar = function() { alert('Its a bar'); };
var x = Foo;
x.bar();

instead of

var Foo = function(){};
Foo.prototype.bar = function(){alert('Its a bar');};
var x = new Foo();
x.bar();

Both of these implementations do the same thing. How is one different from the other? Does this affect inheritance in any way?

  • 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-28T07:16:05+00:00Added an answer on May 28, 2026 at 7:16 am

    When you use the prototype pattern, only one instance of attributes you add to the prototype exist.

    // Lets create 1000 functions which do the same thing
    for (var i=0;i<1000;i++) {
        Foo = new Object();
        Foo.bar = function() { alert('Its a bar'); };
    
        var x = Foo;
        x.bar();
    }
    
    // This is the same as #1, but is more common
    function Foo() {
        this.bar = function () { alert('It\'s a bar'); };
    }
    for (var i=0;i<1000;i++) {
        var x = new Foo;
        x.bar();
    }
    
    // Lets create 1 function
    var Foo = function(){};
    Foo.prototype.bar = function(){alert('Its a bar');};
    for (var i=0;i<1000;i++) {
        var x = new Foo();
        x.bar();
    }
    

    The prototype pattern has the disadvantage of not being able to access private members.

    // Lets create 1 function
    var Foo = function(){
        var private = 4;
    
        this.baz = function () {
            alert(private);
        }
    };
    Foo.prototype.bar = function(){alert(private);};
    
    var x = new foo;
    x.bar(); // error; `private` is undefined
    x.baz(); // ok
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read about Crockford's push for using JavaScript in a more obviously prototypal
I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes
I just watched an introductory JavaScript lecture from Douglas Crockford, in which he mentions
I have read Crockford's JavaScript: The Good Parts and have used his validator JSLint
I read through Crockford's JavaScript best practise , and he stated: There is no
I read this PHP RegEx page , but either I'm missing something, misreading something,
Please read the whole question. I'm not looking for an approach to managing multi-lingual
I've just read The Good Parts, and I'm slightly confused about something. Crockford's example
Read this question today about safe and unsafe code I then read about it
Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .

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.