Skip navigation

Category Archives: Javascript


Today, we are going to expose some inner working of angular 2.

You know what, the double curly braces aka interpolation always  seems awesome magic.

<h1>{{ text }} </h1>

Really, this is sugar syntax for:

<h1 [innerText]=”text”></h1>

Yeah… angular 2 property binding using DOM attribute directly. So, is it possible to add class to a div tag?? Well

<h1 [className]=”active”></h1>

This is equivalent to

h1 = document.querySelector(‘h1’);
h1 = ‘active’;  // className is property of DOM element div

Interesting, isn’t it?

Advertisements

Pass by value and pass by reference is age old concept. It is working similar way in JavaScript too. If you like to see some code so that you can understand better. Keep reading.

Brief theory before jump into code. JavaScript primitive types (string, number, boolean, null and undefined) are passed by value. Whereas non-primitive types (objects, arrays, functions, etc.) are passed by reference.

// Primitive types: Immutable by default
let x = 5
let y = x
y = 10
console.log(x) // output 5
console.log(y) // output 10

// Non-primitive types: Mutable
let animal = {
    name: 'Rat'
}
let anotherAnimal = animal
anotherAnimal.name = 'Horse'
console.log(animal) // {name: "Horse"}
console.log(anotherAnimal) // {name: "Horse"}
console.log(animal === anotherAnimal) // true

This knowledge might be useful while learning react.js, redux architecture.


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
//