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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:49:15+00:00 2026-05-29T03:49:15+00:00

Learning Javascript and have a question about global variables. From my reading, most recommend

  • 0

Learning Javascript and have a question about global variables. From my reading, most recommend not to use them. However, in class based javascripting, does this unwritten rule still apply? For instance:

var width = 0;
var height = 0;

<!-- constructor -->
function Rectangle(){}

<!-- getters/setters -->
Rectangle.prototype.getWidth = function(){
    return width;   
}

Rectangle.prototype.setWidth = function(w){
    width = w;  
}

Rectangle.prototype.getHeight = function(){
    return height;  
}

Rectangle.prototype.setHeight = function(h){
    height = h; 
}

<!-- methods -->
Rectangle.prototype.area = function(){
    return height * width;  
}

var myRect = new Rectangle();
myRect.setWidth(5);
myRect.setHeight(4);
console.log(myRect.area()); //20
console.log(myRect.getWidth()); //5
myRect.setWidth(10);
console.log(myRect.getWidth()); //10
console.log(myRect.area()); //40

I’m familiar with Java and the ability to use access modifiers for classes, properties and methods. Is it because access modifiers do not exist in Javascript that globals should be avoided?

  • 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-29T03:49:16+00:00Added an answer on May 29, 2026 at 3:49 am

    You would probably use this code instead

    <!-- constructor -->
    var Rectangle = function(w, h) {
        this.width = w || 0;
        this.height = h || 0;
    }
    
    <!-- getters/setters -->
    Rectangle.prototype.getWidth  = function( ) { return this.width;  }
    Rectangle.prototype.setWidth  = function(w) { this.width = w;    }
    Rectangle.prototype.getHeight = function()  { return this.height;      }
    Rectangle.prototype.setHeight = function(h) { this.height = h;    }
    
    <!-- methods -->
    Rectangle.prototype.area = function(){
        return this.height * this.width;  
    }
    
    var myRect = new Rectangle(5, 4);
    console.log(myRect.area()); //20
    console.log(myRect.getWidth()); //5
    myRect.setWidth(10);
    
    1. width and height should be this.width and this.height, otherwise you will modify the global width and height variables and those values won’t be tied to the single instance of Rectangle.
    2. it’s better use a function expression than a function declaration for the constructor function: var Rectangle = function(w, h) { ... } rather than function Rectangle() { ... }.
    3. You can also wrap all code inside an immediately self executed function so to completely avoid the global namespace pollution (look at jAndy answer)
    4. Initial width and height of your object can be passed in the constructor function for initialization (otherwise width and height are set to 0 – or other any value – by default).
    5. These rule are definitely written (see for reference Javascript : the good parts by D.Crockford).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been broadening my horizons learning javascript and I have a quick question about
I am learning Javascript, I have been using PHP for about 10 years so
I am learning about Progressive Enhancement and I have a question about AJAXifying views.
I am learning to create addons for Firefox and I have a question about
I have been learning more and more javascript; it's a necessity at my job.
I have just started learning Jquery and am new to writing javascript (I am
I have just started writing my own JavaScript Framework (just for the learning experience),
I am doing some self learning about Patern Matching in Javascript. I got a
I keep reading about defficiencies and issues with languages. Specifically, I'm learning PHP and
Learning from my broad question here ... I was wondering how I could go

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.