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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:55:44+00:00 2026-05-26T20:55:44+00:00

Reading ‘Javascript The good Parts’ from Crocksford, I’ve been in a quest to write

  • 0

Reading ‘Javascript The good Parts’ from Crocksford, I’ve been in a quest to write some efficient, readable and reusable JavaScript code.
That’s all I aim, and particularly like the Object’s properties being public.

That being said, is there any particular problem or point of improvement to this code?

( function(){

    window.App = {}

    // define uma pessoa normal
    App.Person = function(name){
        this.name = name || "anon"
        this.iq = 100
        this.fortune_number = Math.floor(Math.random()*100)
    }

    App.Person.prototype.sayName = function() {
        return (this.name+"!")
    }

    App.Person.prototype.sayIQ = function() {
        return (this.iq)
    }



    // define um genio da humanidade
    App.Genius = function(name) {
        App.Person.call( this, name )
        this.iq = 9000
    };
    // inherits All methods from Pessoa
    App.Genius.prototype = App.Person.prototype

    App.Genius.prototype.solveNP = function() {
        return "eureka!"
    };

})()


var p = new App.Person('Jon')
console.log( "A Person:", p.sayName(), p.fortune_number, p.sayIQ() )
//-> A Person: Jon! 10 100

var g = new App.Genius( 'Eugenios' )
console.log( "A Genious:", g.sayName(), g.fortune_number, g.sayIQ(), g.solveNP() )
//-> A Genious: Eugenios! 7 9000 eureka!

I’m particularly unsure if this line App.Genius.prototype = App.Person.prototype is good, because I usually see prototyping after some new instance, like in Mozilla Guide

  • 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-26T20:55:44+00:00Added an answer on May 26, 2026 at 8:55 pm

    There is no true inheritance in your code. You HAVE TO extend the prototype chain in order to have true (prototypal) inheritance.

    // This is how it's done in JS
    Child.prototype = new Parent();
    Child.prototype.constructor = Child;
    

    This has the side-effect of executing the Parent constructor which can be bad in some cases (for instance if the constructor expects any parameters). You should use the following bit to do the inheritance.

    var Fn = function () {}; // empty constructor, no side-effects
    Fn.prototype = Parent.prototype;
    
    Child.prototype = new Fn();
    Child.prototype.constructor = Child;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through this question on multi-threaded javascript, I was wondering if there would be
reading excel files from C# working well in 32 bit version server. It is
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading MSDN, float ranges from 1E-45 to 1E38, double ranges from 1E-324 to 1E308,
Reading up on design patterns, and have found that there are 23(?) some-odd patterns
Reading the FTP RFC (RFC959), I notice some modes that I've never seen used,
Reading some articles, told me that soft keys varies between devices. Some says -6
Reading some related questions made me think about the theoretical nature of HTML. I'm
Reading some articles, it is best to have POJO object to do JSON or
Reading the questions here and here has given me some insight into the situation,

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.