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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:39:19+00:00 2026-05-19T12:39:19+00:00

Can you please explain the difference between two codes mentioned below ? function Person(){}

  • 0

Can you please explain the difference between two codes mentioned below ?

function Person(){} 
Person.prototype.dance = function(){}; 

function Ninja(){} 

Ninja.prototype = Person.prototype; 

and

function Person(){} 
Person.prototype.dance = function(){}; 

function Ninja(){} 


Ninja.prototype = new Person();

I am little confused at these lines:

Ninja.prototype = Person.prototype; 

and

Ninja.prototype = new Person();

I came to know the second one supports Inheritance and the first one not, Can you explain me what is the magic in the second one?

  • 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-19T12:39:20+00:00Added an answer on May 19, 2026 at 12:39 pm
    1. Setting Ninja.prototype = Person.prototype; is saying that all Ninjas are Persons, and all Persons are Ninjas, since it simply makes the two prototypes point to the same thing. So changing Ninja.prototype will change Person.prototype and vice versa.

    2. Setting Ninja.prototype = new Person(); is saying that all Ninjas start off being a regular person, but Ninja.prototype can be modified without changing the definition of Person. The key here is the new keyword, which creates a unique instance of Person, and is therefore free to be modified without affecting anything else.


    Example of Ninja.prototype = Person.prototype

    Define Ninja‘s prototype to be the same as Person’s:

    function Person() {}
    Person.prototype.dance = function () {}; // A Person can dance
    
    function Ninja() 
    Ninja.prototype = Person.prototype; // Now a Ninja can dance too!
    

    An instance of Ninja has the abilities of Person:

    var ninja = new Ninja();
    ninja.dance();
    

    But, modifications to the definition of Ninja also affect instances of Person:

    Ninja.prototype.kill = function () {}; // Oh no! Now a Person can kill too!
    var bob = new Person();
    bob.kill(); // Not what we wanted...
    

    Example of Ninja.prototype = new Person()

    Define Person in the same way as before:

    function Person(){};
    Person.prototype.dance = function () {}; // A Person can dance
    

    Now I’ll break Ninja.prototype = new Person() into two steps. First, create a new Person, called defaultNinja:

    var defaultNinja = new Person(); // Despite the name, it's just a regular Person
    

    Then define all Ninjas to be like the default:

    function Ninja(){};
    Ninja.prototype = defaultNinja; // Really the same as Ninja.prototype = new Person();
    

    This time if we change what Ninjas can do:

    Ninja.prototype.kill = function () {};
    // OR,
    defaultNinja.kill = function () {};
    

    Instances of Person aren’t affected:

    ninja.kill(); // Now the ninja can kill
    var bob = new Person();
    bob.kill(); // ERROR, because Person.prototype doesn't have kill(),
                // only defaultNinja does
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain the difference between the following two options of Tortoise SVN
Can someone please explain me what's the difference between Swing and AWT? Are there
Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections
Can somebody please explain (with example) the difference between context-independent and context-dependent overloading?
can somebody please explain what is the difference between the following mechanisms of passing
In WPF: Can someone please explain the relationship between DependencyProperty and Databinding? I have
Could someone please explain the significant difference in speed between a firefox updatepanel async
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please explain what the & does in the following: class TEST {

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.