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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:17:26+00:00 2026-06-07T11:17:26+00:00

I am trying to understand JavaScripts Prototypal nature and I am trying to create

  • 0

I am trying to understand JavaScripts Prototypal nature and I am trying to create object inheritance without using class like constructor functions.

How can I attach the prototype chain from Animals and then each to Cat and Dog if they are all three object literals?

Also, is there a way to do an Object.create and add more properties in literal form (like myCat).

I’ve added the code below & to paste bin http://jsbin.com/eqigox/edit#javascript

var Animal = {
    name        : null,
    hairColor   : null,
    legs        : 4,

    getName : function() {
        return this.name;
    },
    getColor : function() {
        console.log("The hair color is " + this.hairColor);
    }
};

/* Somehow Dog extends Animal */

var Dog = {
    bark : function() {
        console.log("Woof! My name is ");
    }
};

/* Somehow Cat Extends Animal */

var Cat = {
    getName : function() {
        return this.name;
    },

    meow : function() {
        console.log("Meow! My name is " + this.name);
    }
};


/* myDog extends Dog */

var myDog = Object.create(Dog);

/* Adding in with dot notation */
myDog.name = "Remi";
myDog.hairColor = "Brown";
myDog.fetch = function() {
    console.log("He runs and brings back it back");
};


/* This would be nice to add properties in litteral form */
var myCat = Object.create(Cat, {
    name        : "Fluffy",
    hairColor   : "white",
    legs : 3, //bad accident!
    chaseBall : function(){
        console.log("It chases a ball");
    }

});

myDog.getColor();
  • 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-07T11:17:27+00:00Added an answer on June 7, 2026 at 11:17 am

    You can use Object.create to use your defined objects as prototypes. For instance:

    var Dog = Object.create(Animal, {
        bark : {
            value: function() {
                console.log("Woof! My name is " + this.name);
            }
        }
    });
    

    Now you can create a new Dog object:

    var myDog = Object.create(Dog);
    myDog.bark();  // 'Woof! My name is null'
    myDog.getColor();  // 'The hair color is null'
    

    Example: http://jsfiddle.net/5Q3W7/1/

    Alternatively, if you’re working in the absence of Object.create, you can use constructors:

    function Animal() {
        this.name = null;
        this.hairColor = null;
        this.legs = 4;
    };
    
    Animal.prototype = {
        getName : function() {
            return this.name;
        },
        getColor : function() {
            console.log("The hair color is " + this.hairColor);
        }
    }
    
    function Dog() {
    }
    
    Dog.prototype = new Animal;
    Dog.prototype.bark = function() {
        console.log("Woof! My name is " + this.name);
    };
    

    Example: http://jsfiddle.net/5Q3W7/2/

    For more information about Object.create: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/

    For more information about Constructors and prototypes chains:
    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor

    https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_and_the_prototype_chain

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

Sidebar

Related Questions

Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method I am trying to understand object in JavaScript.
I am trying to understand the Javascript event handling. I like some code to
I'm trying to understand how to build a JSON object in JavaScript. This JSON
I am trying to understand javascript octal and hexadecimal computations. I know I can
I am trying to understand callback functions in javascript. There is a function something
I'm coming from a Java background, with its class-based inheritance model, trying to get
I'm trying to understand when to use anonymous JavaScript functions. State differences between the
I'm trying to understand how my PHP script can pass an array to my
I rather like the prototype way of programming and have been trying to understand
I'm just trying to understand Javascript a little deeper. I created a 'class' gameData

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.