Skip navigation

Tag Archives: shock

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
({}), 'bar'); // true

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

Learning experience with JavaScript is more excited day-by-day.

(function(undefined) {
    alert(typeof undefined);   // boolean
(function(undefined) {
    alert(typeof undefined);   // undefined

Refer jQuery source code (development version 1.7.1) and modernizr, the first line would be

(function( window, undefined ) {

})( window );
window.Modernizr = (function( window, document, undefined ) {

})(this, this.document);

To overcome the issue of undefined, these libraries passing undefined as local variable. Notice the third parameter is left blank.

Read my another post regarding the same topic.

JavaScript have some odd behaviour on object property names. If you know this for first time, you will be shocked. Programmers from other language background will be surprised to know this kind of JavaScript behaviour.

// declare a variable and create a new object with property name and value raja

var myO = {name:"raja"};
alert(;  //raja

Your shocks starts here…

myO.if = "use reserved word";
alert(myO.if);  //use of reserved word

myO["if else if && || *^ "] = "you can use anything";
alert(myO["if else if && || *^ "]);  //you can use anything

myO[""] = "Surprise";  // no error. even empty is allowed
alert(myO[""]);  // Surprise

Similar article written by mathias