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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:32:37+00:00 2026-06-09T05:32:37+00:00

I wonder what is the difference between these two approaches. They both work but

  • 0

I wonder what is the difference between these two approaches. They both work but I don’t understand if the 2nd approach might have undesirable implications?

// A. Putting a prototype method outside the function declaration (what I would normally do)
var Cat = function(){
}

Cat.prototype.eat = function(){
    // implementation
}

// B. Putting a prototype method inside the function declaration (it works too but the scoping seems different)
var Cat = function(){

    Cat.prototype.eat = function(){
        // implementation
    }   
}
  • 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-09T05:32:38+00:00Added an answer on June 9, 2026 at 5:32 am

    Every object has a prototype. Prototypical inheritance allows you to assign either an entirely new prototype (similar to the classical inheritance):

    function Animal() {
        this.numLegs = 4;
    }
    function Cat() {
        // Implementation
    }
    Cat.prototype = new Animal();
    
    var kitten = new Cat();
    console.log(kitten.numLegs); // 4
    

    Or directly add variables and methods to the current class’s prototype:

    function Cat() {
        // Implementation
    }
    Cat.prototype.numLegs = 4;
    
    var kitten = new Cat();
    console.log(kitten.numLegs); // 4
    

    Your second example simply reassigns the eat function to the Cat prototype every time the Cat class is initiated, which is useless but doesn’t take up any memory because it just overwrites the old value.

    Why is this useful? Remember that functions are objects. For every instance of your class, each variable and function defined in that class takes up it’s own memory. Using prototypical inheritance, you can share common methods, thus not taking up the extra memory for each instance.

    Why is this not as useful? You don’t have access to private variables.

    Keep in mind that this is not the same thing as a static method, which can be declared as:

    Cat.feed = function(kittens) {
        for (var kitten in kittens) {
            kitten.eat();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder what is the difference between these two: gcc -s : Remove all
i wonder what the difference is between these two declarations: var delay = (function()
Hey I'm wonder what's the difference between these 2 codes? The first works but
There is no difference between these two lines, because the compiler, in the second
The following string of mine tried to find difference between two strings. But it's
I wonder now, what is the difference between the two methods: DataInputStream.skipBytes and DataInputStream.skip
I'm quite new to Oracle's ADF and wonder what is the difference between ADF
I wonder how apps like these generate screenshots for different browsers. Are they using
i wonder if there is any difference between class TestClass { private $_var =
I have heard people using these terms. I wonder if they refer to the

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.