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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:25:40+00:00 2026-05-14T01:25:40+00:00

I have a simple code fragment in JS working with prototype inheritance. function object(o)

  • 0

I have a simple code fragment in JS working with prototype inheritance.

function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

//the following code block has a alternate version
var mammal = {
    color: "brown",
    getColor: function() {
        return this.color;
    }
}

var myCat = object(mammal);
myCat.meow = function(){return "meow";}

that worked fine but adding this:

mammal.prototype.kindOf = "predator";

does not. (“mammal.prototype is undefined”)

Since I guessed that object maybe have no prototype I rewrote it, replacing the var mammal={… block with:

function mammal() {
    this.color = "brown";
    this.getColor = function() { return this.color; }
}

which gave me a bunch of other errors:

“Function.prototype.toString called on incompatible object”
and if I try to call _myCat.getColor()
“myCat.getColor is not a function”


Now I am totally confused. After reading Crockford, and Flanagan I did not get the solution for the errors. So it would be great if somebody knows…

– why is the prototype undefined in the first example (which is foremost concern; I thought the prototype of explicitly set in the object() function)

– why get I these strange errors trying to use the mammal function as prototype object in the object() function?

Edit by the Creator of the Question: These two links helped a lot too:

Prototypes_in_JavaScript on the spheredev wiki explains the way the prototype property works relativily simple. What it lacks is some try-out code examples. Some good examples are provided by Morris John’s Article. I personally find the explanations are not that easy as in the first link, but still very good.
The most difficult part even after I actually got it is really not to confuse the .prototype propery with the internal [[Prototype]] of an object.

  • 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-14T01:25:40+00:00Added an answer on May 14, 2026 at 1:25 am

    You get the first error (mammal.prototype is undefined) because mammal is an object, the prototype property is added to function objects when they are created and it should be used when you want to have functions as constructors.

    I think you are confusing that property with the internal [[Prototype]] property.

    The [[Prototype]] property can only be set by the new operator, through the [[Construct]] internal operation.

    This property is not accessible, (although there are some ways, like in the Mozilla implementation by obj.__proto__; or the new ECMAScript 5 Object.getPrototypeOf method).

    About your second question, you get those errors because you are creating a new object, that inherits from a function, not from another object.

    What about :

    var mammal = {
      color: "brown",
      getColor: function(){
        return this.color;
      },
      kindOf: "mammal" // "base" value
    };
    // ...
    var tiger = object(mammal);
    tiger.roar = function(){return "roar";}
    tiger.kindOf = "predator"; // specific value
    

    In the above snippet, all the instances that inherit from mammal, will have a kindOf property, that you can later change when you create more specific mammal objects.

    Edit: In response to your comment, yes, the language itself gives you the tool to know that, the Object.prototype.hasOwnProperty method, it returns a boolean result, indicating whether the object has physically the specified property or not, e.g.:

    var obj = {
      foo: 'bar'
    };
    
    obj.hasOwnProperty('foo'); // true
    obj.hasOwnProperty('toString'); // false, inherited from Object.prototype
    

    You can also call this method directly on the Object.prototype, so if someone names a property hasOwnProperty on an object, it won’t fail:

    Object.prototype.hasOwnProperty.call(obj, 'foo'); // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 350k
  • Answers 350k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well to make a long story short, I don't have… May 14, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer There are basically two approaches. In ActionScript (and many other… May 14, 2026 at 7:00 am
  • Editorial Team
    Editorial Team added an answer You set up a singular route when you used the… May 14, 2026 at 7:00 am

Related Questions

I have simple AJAX function that uses jQuery to return an array of 300
I have a simple problem that says: A password for xyz corporation is supposed
I'm working on a project that needs Jasper reporting, I have used the code
Consider the following code fragment in VS2010 Beta 1: let array = Array2D.zeroCreate 1000
I'm using jQuery to replace an iframe sort of environment on a page by

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.