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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:21:27+00:00 2026-06-01T05:21:27+00:00

I have written a constuctor function Human() { var legs = 2; var iq

  • 0

I have written a constuctor

function Human() { 
var legs = 2;
var iq = 100; 
}

Then I create an instance of the object

var Man = new Human();

And want to add a new method

Man.getIQ = function() {
return iq - 10;
}

But I’m told that IQ is undefined. Even if I use this.iq.
Why isn’t a var inside the scope of an object available for new methods?

  • 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-01T05:21:28+00:00Added an answer on June 1, 2026 at 5:21 am

    The variables legs and iq are simulating a “private” member to the Human class because they are visible only in that closure (only in the Human function).
    If you want to access them from outside that scope, you need to make them public by binding them to the this keyword (this.iq=100;) or by implementing getters and setters for each of your private member :

    function Human() { 
        var legs = 2;
        this.getLegs = function(){
            return legs;
        };
    }
    

    Anyway, these are represent just the very tip of the iceberg; I explained them so that you can understand why what you were trying to do failed.
    If I understand correctly what you were trying to do, the right way to write your ideea oop in js would be something like this :

    function Human(){}
    Human.prototype = {
        legs : 2,
        iq : 100
    };  
    
    function Woman(){}
    Woman.prototype = new Human;
    Woman.prototype.getIq = function(){
        return this.eq - 10;
    };
    
    var womanInstance = new Woman();
    // needless to say that this line is both wrong and misogynistic
    alert('a woman has an iq of ' + womanInstance.getIq());
    

    Sorry if I got carried away, but there are lots of articles & blogs about javascript oop (such as this one) that I recommend you read before/while getting stuck with these kind of problems.

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

Sidebar

Related Questions

I have written a site in Prototype but want to switch to jQuery. Any
I have written an assembly I don't want other people to be able to
I'm quite new to programming larger applications. I have now written a command line
I have an object written like this: Object1.prototype = { isInit: false, Get :
I have written an ml function and in the output i am getting out
I have an instance of a logging class logger, this class has a function
I have never written copy constructor, so in order to avoid pain i wanted
I have written an AIR Application that downloads videos and documents from a server.
I have written some code in my VB.NET application to send an HTML e-mail
I have written a ruby script which opens up dlink admin page in firefox

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.