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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:35:08+00:00 2026-06-02T20:35:08+00:00

I think the difference has clicked in my head, but I’d just like to

  • 0

I think the difference has clicked in my head, but I’d just like to be sure.

On the Douglas Crockford page Prototypal Inheritance in JavaScript, he says

In a prototypal system, objects inherit from objects. JavaScript,
however, lacks an operator that performs that operation. Instead it
has a new operator, such that new f() produces a new object that
inherits from f.prototype.

I didn’t really understand what he was trying to say in that sentence so I performed some tests. It seems to me that the key difference is that if I create an object based on another object in a pure prototypal system, then all the parent parent members should be on the prototype of the new object, not on the new object itself.

Here’s the test:

var Person = function(name, age) {
        this.name = name;
        this.age = age;
}
Person.prototype.toString = function(){return this.name + ', ' + this.age};

// The old way...
var jim = new Person("Jim",13);
for (n in jim) {
    if (jim.hasOwnProperty(n)) {
        console.log(n);
     }
}
// This will output 'name' and 'age'.

// The pure way...
var tim = Object.create(new Person("Tim",14));
for (n in tim) {
    if (tim.hasOwnProperty(n)) {
        console.log(n);
     }
}
// This will output nothing because all the members belong to the prototype.
// If I remove the hasOwnProperty check then 'name' and 'age' will be output.

Is my understanding correct that the difference only becomes apparent when testing for members on the object itself?

  • 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-02T20:35:10+00:00Added an answer on June 2, 2026 at 8:35 pm

    Your assumptions are correct, but there is another pattern that Douglas doesn’t talk much about – the prototype can be used for properties as well. Your person class could have been written as:

    var Person = function(name, age) {
        this.name = name;
        this.age = age;
    }
    Person.prototype.name = null; //default value if you don't init in ctor
    Person.prototype.age = null;
    Person.prototype.gender = "male";
    Person.prototype.toString = function(){return this.name + ', ' + this.age;};
    

    In this case, iterating over properties of an instance of this class, as you do in your example, would generate no output for the ‘gender’ property.

    EDIT 1:
    The assignment of name and age in the constructor do make the properties visible by hasOwnProperty (thanks @matt for reminding me of this). The unassigned gender property would not be visible until someone sets it on the instance.

    EDIT 2:
    To further add to this, I present an alternative inheritance pattern – one that I have personally used for very large projects:

    var inherits = function(childCtor, parentCtor) {
      function tempCtor() {};
      tempCtor.prototype = parentCtor.prototype;
      childCtor.superclass = parentCtor.prototype; 
      childCtor.prototype = new tempCtor();
      childCtor.prototype.constructor = childCtor;
    };
    
    var Person = function(name){
        this.name = name;
    }
    Person.prototype.name = "";
    Person.prototype.toString = function(){
        return "My name is " + this.name;
    }
    
    var OldPerson = function(name, age){
        OldPerson.superclass.constructor.call(this);
        this.age = age
    };
    inherits(OldPerson, Person);
    OldPerson.prototype.age = 0;
    OldPerson.prototype.toString = function(){
        var oldString =  OldPerson.superclass.toString.call(this);
        return oldString + " and my age is " + this.age;
    }
    

    This is a fairly common pattern with a small twist – the parent class is attached to the child via the “superclass” property permitting you to access methods/properties overridden by the child. Technically, you could replace OldPerson.superclass with Person, however that is not ideal. If you ever changed OldPerson to inherit from a class other than Person, you would have to update all references to Person as well.

    EDIT 3:
    Just to bring this full circle, here is a version of the “inherits” function which takes advantage of Object.create and functions exactly the same as I previously described:

    var inherits = function(childCtor, parentCtor) {
        childCtor.prototype = Object.create(parentCtor.prototype);
        childCtor.superclass = parentCtor.prototype; 
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this topic has been discussed but I think it has some differences.
So I (think I) understand the difference between Float, Double, and Decimal , but
i'm pretty sure that this question has allready been asked (but i didn't find
I realize there's already been several questions like this, but I think my case
OK I think I get Difference between jQuery.extend and jQuery.fn.extend? in that the general
What is the difference between com.sun.lwuit.Form.show() and com.sun.lwuit.Form.showBack() ? I think both are displays
Using the release version of Visual Studio 2010 I think there's a difference in
As far as I can tell I wouldn't think it would make any difference
There are many errors here in SO, but this scenario I think its different.
I need to build a web page with a DetailsView which has a bunch

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.