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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:09:28+00:00 2026-05-27T09:09:28+00:00

Possible Duplicate: Use of 'prototype' vs. 'this' in JavaScript? OK, So I am somewhat

  • 0

Possible Duplicate:
Use of 'prototype' vs. 'this' in JavaScript?

OK, So I am somewhat new to the idea of OOP in JS.

What is the difference between these two snippets of code written below:

function animal(){
    this.name = 'rover';
    this.set_name = function(name){
         this.name = name;
    }
}
function animal(){
    this.name = 'rover';
}
animal.prototype.set_name = function(name){
    this.name = name;
}

They both do the same thing, so what’s the difference?

  • 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-27T09:09:29+00:00Added an answer on May 27, 2026 at 9:09 am

    Using the prototype makes faster object creation since properties/methods on the prototype don’t have to be re-created each time a new object is created.

    When you do this:

    function animal() {
        this.name = 'rover'
        this.set_name = function (name) {
          this.name = name
        }
    }
    

    The set_name method is created every time you create an animal. But when you do this

    animal.prototype.set_name = function (name) {
        this.name = name
    }
    

    The method does not have to be re-created each time; it exists in one place in the prototype. So when you call someAnimal.set_name("Ubu"); the this context will be set to someAnimal and (the one and only) set_name method will be called.


    There is one advantage to using the first syntax though: methods created in this manner will have access to private data:

    function animal() {
        var privateData = 'foo'
    
        this.name = 'rover'
        this.set_name = function (name) {
            this.name = name
            alert(privateData) //will alert 'foo'
        }
    }
    

    Douglas Crockford calls methods created like this "privileged" for that reason: they have access to both public and private data.

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

Sidebar

Related Questions

Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I've seen both these two
Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I am confused with these
Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method What's the difference between creating a prototype like
Possible Duplicate: Use of 'prototype' vs. 'this' in Javascript? I went to various websites
Possible Duplicate: Why use 'self.' when its not forced? What is the difference between
Possible Duplicate: When should I use a javascript framework library? All, I'm fairly new
Possible Duplicate: How does JavaScript .prototype work? What is the use of prototype property
Possible Duplicate: Use of .apply() with 'new' operator. Is this possible? I have 5
Possible Duplicate: Why use two stacks to make a queue? I got this assignment
Possible Duplicate: Escape string for use in Javascript regex I have a msg like

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.