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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:57:31+00:00 2026-05-31T03:57:31+00:00

I am wondering about javascript prototype. I know that prototyping enables the user to

  • 0

I am wondering about javascript prototype. I know that prototyping enables the user to add new properties and methods to a constructor or an object.

I have used the following several times:

function Example() {}
Example.prototype.add = function () {};
Example.prototype.sub = someVar;

This way I can use Example.add as a function and Example.sub as an element of object.
But happens when someone writes something like this

Example.prototype = function() {
                    //Code
                    };

Any help?

  • 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-31T03:57:33+00:00Added an answer on May 31, 2026 at 3:57 am

    Let’s take a look at a simple example:

     var Person = function(args){
         this.init(args);
     }
    

    When defining a few methods for the person prototype we would do this:

     Person.prototype = {
         getAge : function(){},
         nickname : "R2D2"
     };
    

    Or

     Person.prototype.getAge = function() {...};
     Person.prototype.nickName= "R2D2";
    

    Both do the same thing. I prefer the first version because it’s easier to read and needs less characters to write ( making the footprint of my code smaller ).

    The only reason I could think of for using

     Person.prototype = function(){}
    

    is to create a private scope available only to the prototype of the Person. But this requires a self executing function which returns an object.

     Person.prototype = (function(){
        var theAnswerToEverything = 42;
        return {
            getTheAnswerToEverything : function(){
               return theAnswerToEverything;
            }
        };
     })();
    

    theAnswerToEverything will be a variable usable only by the methods of the Persons prototype. More complex examples could be created by using the same principle.

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

Sidebar

Related Questions

I'm pretty new to javascript and am wondering about quoting the keys in object
Just wondering about this... I have several separate javascript files, that all contain code
I'm wondering about the behind the scenes magic that's happening when you create a
I admit, I don't know too much about javascript, mostly I just steal and
So I know javascript pretty well, but I'm not sure about this one. Tough
I was wondering about Javascript performance about using string.replace() or string.substr(). Let me explain
I was wondering how i would go about changing image.src in the javascript without
I've been thinking about javascript programming style, and I was wondering whether it makes
I saw this question and I am wondering about the same thing in JavaScript.
I have an app that has a bunch of resources. Having read about javascript

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.