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

The Archive Base Latest Questions

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

The example below shows a class that extends a goog.ui.Component. Should the properties of

  • 0

The example below shows a class that extends a goog.ui.Component.

Should the properties of a class be defined outside of the constructor as shown below, or should they be defined inlined in the constructor only?

Is it ok to initialize properties to null?

goog.provide("org.something.SomeClass");

/**
 * @type {Object}
 * @private
 **/
org.something.SomeClass.prototype.anObject_ = null;

/**
 * @type {Element}
 * @private
 **/
org.something.SomeClass.prototype.anElement_ = null;

/**
 * @param {goog.dom.DomHelper=} opt_domHelper
 * @constructor
 * @extends {goog.ui.Component}
*/
org.something.SomeClass = function () {
    goog.ui.Component.call(this, opt_domHelper);

    this.anObject_ = {};
    this.anElement_ = new Element();
};
goog.inherits(org.something.SomeClass, goog.ui.Component);
  • 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-10T14:44:20+00:00Added an answer on June 10, 2026 at 2:44 pm

    The Closure Library implements classes with properties defined on the prototype as well as within the constructor function. In addition to studying the Closure Library source code, here are some questions to consider when deciding whether to define a property on the prototype or in the constructor.

    Is the property unique for each instance of the class?

    Properties that are unique for each instance (such as a car’s VIN number) should not be shared between instances and are therefore defined in the constructor and not on the prototype.

    /**
     * A car.
     * @param {string} vin The Vehicle Identification Number.
     * @constructor
     */
    Car = function(vin) {
    
      /**
       * The Vehicle Identification Number.
       * @type {string}
       * @private
       */
      this.vin_ = vin;
    }; 
    

    Is the property type immutable (e.g. string, boolean, number) or mutable (e.g. Object, Array)?

    Since immutable property types may be safely shared between instances, they should be defined on the prototype. If an instance needs to override the shared default value, an instance property may be added that shadows the prototype property of the same name.

    /**
     * The number of cylinders in the engine.
     * @type {number}
     * @private
     */
    Car.prototype.cylinders_ = 4;
    
    /**
     * Sets the number of cylinders in the engine.
     * @param {number} cylinders The number of cylinders.
     */
    Car.prototype.setCylinders = function(cylinders) {
      if (this.cylinders_ == cylinders) {
        // Since the number of cylinders has not changed, do not add a new
        // instance property to shadow the prototype property. Instead, continue
        // to use the prototype property.
        return;
      }
    
      // Defines cylinders_ property on the instance object that shadows
      // Car.prototype.cylinders_
      this.cylinders_ = cylinders;
    };
    
    /**
     * Gets the number of cylinders in the engine.
     * @return {number} The number of cylinders.
     */
    Car.prototype.getCylinders = function() {
      return this.cylinders_;
    };
    

    The following example illustrates how setting the instance property shadows the prototype property.

    var myCar = new Car("1HGCM82633A004352");
    alert(myCar.getCylinders()); // Alerts 4.
    
    myCar.setCylinders(6);
    alert(myCar.getCylinders()); // Alerts 6;
    
    // Delete the instance property and let the prototype property
    // "shine through".
    delete myCar.cylinders_;
    alert(myCar.getCylinders()); // Alerts 4;
    

    If the property type is mutable, such as Array or Object, then in most cases you would not want to share the same mutable property instance between instances of the class. Hence, mutable properties are more often defined in the constructor and not on the prototype.

    Is it ok to initialize properties to null?

    There are many examples in the Closure Library where properties are initialized to null. However, it is better to initialize variables to useful default values when possible, such as setting the default number of cylinders to 4 as in the example above.

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

Sidebar

Related Questions

The example below shows the code I am using to test whether a user
I have a SQLite table called posts . An example is shown below. I
The example below, is just an example, I know that I don't need an
example: import scala.actors._ import Actor._ class BalanceActor[T <: Actor] extends Actor { val workers:
Given the example below, can someone please show me how this could be called?
I've seen many examples of pagination like what's shown below. However, I want to
Reproducible example below. I have a simulation loop, within which I occasionally have rows
this example below works when hover event is trigered and when its not, its
JSFIDDLE EXAMPLE BELOW I want to make the following CSS layout and currently im
The example below is from a REST database driver on Python 2.7. In the

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.