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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:43:28+00:00 2026-05-24T12:43:28+00:00

I play javascript with the book Professional JavaScript for Web Developers . I practice

  • 0

I play javascript with the book Professional JavaScript for Web Developers. I practice an example in section 6.2.6. the codes are listed below:

function creatPrototype(subType, superType) 
{
    function subTypePrototype(){};
    subTypePrototype.prototype = superType.prototype;
    subTypePrototype.constructor = subType;
    subTypePrototype.str = "say";
    return new subTypePrototype();
}

function Person(name, age) 
{
    this.name = name;
    this.age = age;
}
Person.prototype.say = function(){
    writeln("bill say");
}


function itMan(name, age){
    Person.apply(this, arguments); 
}
itMan.prototype = creatPrototype(itMan, Person); 


var Bill = new itMan("bill", 25);

writeln(itMan.prototype.str);    //expect "say"
writeln(Person.prototype == itMan.prototype.prototype);   //expect true
Bill.say();  //expect "bill say"

the result is:

undefined

False

bill say

Why?

  1. itMan.prototype.str is suppose to "say"

  2. Person.prototype AND itMan.prototype.prototype should point to a same object

  3. Bill.say() run correctly, so the prototype chain is OK.

  • 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-24T12:43:28+00:00Added an answer on May 24, 2026 at 12:43 pm

    You have to think about which property belongs to the constructor function and which one belongs to the instance. prototype is a property of the function, but constructor and str should be both properties of the instance.

    This should do it:

    function createPrototype(subType, superType) 
    {
        function subTypePrototype(){};
        subTypePrototype.prototype = superType.prototype;
    
        var newPrototype = new subTypePrototype();
    
        newPrototype.constructor = subType;
        newPrototype.str = "say";
        return newPrototype;
    }
    

    But, as you are also passong subType, you can actually assign the prototype directly:

    function inherit(subType, superType) 
    {
        function tconstr(){};
        tconstr.prototype = superType.prototype;
    
        subType.prototype = new tconstr();
    
        subType.prototype.constructor = subType;
        subType.prototype.str = "say";
    }
    

    and then just call it with

    inherits(itMan, Person);
    

    Person.prototype AND itMan.prototype.prototype should point to a same object

    Remember that prototype is a property of a function, not of objects. But itMan.prototype is a an object. You cannot access an objects prototype unless you explicitly refer to it (but I would not do so).

    With ECMAScript 5, there is a way to get the prototype, using Object.getPrototypeOf [MDN]. This only works in newer browsers though.

    Here is a working example of your code.

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

Sidebar

Related Questions

I cannot seem to debug my JavaScript code with Firebug. The play button is
I need to play .vox files on my web page. Does anyone have any
Anyone clear on how big a role Javascript will play when HTML 5 comes
how to play and stop sound control by javascript in wmplayer in asp.net ?
A client is looking to play a brief splash javascript animation whenever a user
I'm trying to have JavaScript call a function from my ActionScript. ActionScript: import flash.external.ExternalInterface;
How can I get a CSS Animation to play with a JavaScript onClick? I
Is it possible to play MMS audio stream with HTML5 / JavaScript in Google
I am new with programming, how do play music in Java program Javascript? make
I'd like to play around a bit with pure web app programming. Coming from

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.