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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:18:50+00:00 2026-06-13T14:18:50+00:00

Why does the Cat.prototype = new Mammal() line below not work inside the Cat()

  • 0

Why does the “Cat.prototype = new Mammal()” line below not work inside the Cat() function, but it works outside the Cat() function?

function Mammal() {
  Mammal.prototype.hasHair = function() { return true; }
}

alert( new Mammal().hasHair() ); // dispays true here

function Cat() {
  Cat.prototype = new Mammal();
}

try {
  new Cat().hasHair(); // doesn't work. why?
} catch ( err ) {
  alert('error'); // displays error here
}

Cat.prototype = new Mammal(); // same line as inside Cat() function

alert( new Cat().hasHair() ); // now it works

I tried this with several different javascript implementations, so I doubt it is an implementation idiosyncrasy. I wonder about this mostly out of curiousity, but also just for organization, I would like to define about Cats inside Cat’s function, not spread out all over everywhere.

  • 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-13T14:18:52+00:00Added an answer on June 13, 2026 at 2:18 pm

    Because prototype members are passed to the object instance before constructor is executed.

    http://jsfiddle.net/vSDbB/

    function Mammal() {
      Mammal.prototype.hasHair = function() { return true; }
    }
    
    function Cat() {
        Cat.prototype = new Mammal();
    }
    
    new Cat();
    alert(new Cat().hasHair());
    

    So the code above will work, but if you commented first new Cat(); line it wouldn’t since on the new Cat().hasHair() line Cat’s prototype doesn’t have hasHair() method

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

Sidebar

Related Questions

I`m trying to execute linux commant 'cat' from java code, but it does not
Why does the following not work? exec 3<|cat $0 The idea is to get
Why does this work: exec 3<>/dev/tcp/www.google.com/80 echo -e GET / HTTP/1.1\n\n>&3 cat <&3 And
Not sure why the last line does not cut the from the script: #!/bin/bash
Why this does not work in firefox i try to select the category and
Why does the following not work as expected? $ find .git/objects -type f .git/objects/5a/91f388f3648b98ae34a19ec42ba9acc7852ef4
Suppose I have a function like below: # cat 003.c int foo(int a, int
I have the following call which simply does not work in ie7 or 8
If my gz file does not exist, why doesn't it DIE? $ cat test.pl
Does anyone have a translate function for x/y positions after rotation in javascript? for

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.