Skip navigation


JavaScript keeps giving shock to me even after using, learning since so long years! I just came to know that hasOwnProperty method is not guarded by the JavaScript language. Users can override it. Below code demonstrates how to override, consequences and work around.

//
var foo = {
    // overriding foo's default hasOwnProperty method
    hasOwnProperty: function() {
        return false;
    },
    bar: 'rajakvk'
};

foo.hasOwnProperty('bar'); // false always

// using hasOwnProperty of of object and setting this to foo
({}).hasOwnProperty.call(foo, 'bar'); // true

// or use hasOwnProperty from the 
// Object prototype for this purpose
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
//

Article published in TechAspect’s digital cum print edition of internal company official magazine TechTunes October 2015 issue (Vol 1, 2015)

Rapid and ever-growing browser technologies are a boon to internet users. At the same time, these cutting edge innovations present the developer community with a new set of opportunities and challenges. The autonomous nature of the internet produces conflicting opinions in many areas, such as standards bodies, un-finalized specs, browser differences, and so on; growth of mobile device use further adds fuel to the fire.

Challenges

The ‘party’ is no longer server side but rather now inside the browser, which is controlled by the user. In other words, the browser is the new OS. Frontend developers often find themselves in unexplored territory. The shelf life of frontend technology has shortened considerably in recent years, calling for
developers to realign our strategies.

Impact on Project Success

Our success rate increases if we identify frontend risks well in advance. It is very easy to miss a CSS incompatibility, which results in a broken home page layout. The contents and underlying DOM are so dynamic they may be overlooked.

Mitigation

To avoid costly mistakes, it is important to understand frontend volatility, and to perform due diligence, even though it is challenging. It is also essential to understand clients’ requirements, select the right
technologies, accurately estimate UI costs, and select the right resources.


It is common in forms, to get user inputs for numerical calculations later like house rent allowance for salary calculation. In such scenarios, the value returned is string which if we add result in concatenation. To avoid this mistake adding toInt method to string object directly will be handy.

/**
 * adding toInt method to String object
 * author rajakvk <rajakvk at gmail dot com>
 */
if(!String.toInt) {
  /**
   * Returns integer value if a string value is passed.
   * @name toInt
   * @methodOf String
   * @param {*} none 
   * @returns {Number}
   */
  String.prototype.toInt = function() {
     return parseInt(this, 10);
  }
}
// Usage
// $('#hra').val().toInt();
// typeof $('#hra').val();  // string
// typeof $('#hra').val().toInt();  // number

Note: This method converts string to Integer discarding decimal places.