Saturday, 23 September 2017

Calling The Super Function In JavaScript



With prototypal inheritance in JavaScript how would you call the super version of a function. Consider Dept that inherits from College as below:


Now you wanted to call displayName() on oDept and you could do that as below:


All of this is simple and cool, but what if you wanted to call the displayName() of College as well from the displayName() in Dept? Something as below:


Notice line-27 where there is an attempt to call the super (College) version of the displayName(). This is however a futile exercise here, as the super is undefined and not recognized. We need to find a way to accomplish this though. Take a look at the solution below:


The trick lies in line-35. Notice that we store the reference to the prototype of Dept in the property uber. You may want to call it super or anything else. Finally it allows us to call the super version of displayName at line number 27.

None of this was possible if we didn't ensure that we had line-34 in place though. Read here if you are wondering why!


Saturday, 16 September 2017

Why is typeof NaN a number?


It's NaN (not a number), but it's typeof is "number". No, really!
Try this:

var iamNaN = 100 * "xyz";

Here, iamNaN becomes NaN for obvious reasons. You cannot expect 100* "xyz" to yield a valid number and so iamNaN gets the value NaN.

However, what's interesting is, typeof iamNaN returns "number". The question is, if iamNaN is NaN (not a number) then how on earth can its data type be a number?

typeof iamNaN; // gives "number"
isNaN(iamNaN); // gives true

Also, why is typeof a string not "number"? After all, a string is also a NaN.

var whyNotMe = "hello";
isNaN(whyNotMe); // gives true

typeof whyNotMe; // yields "string"

iamNaN contains a value that is not a number, and so does whyNotMe. Then why is typeof iamNaN a "number" and not that of whyNotMe?

The ECMAScript standard states that Numbers should be IEEE-754 floating point data. This includes Infinity, -Infinity, and also NaN.

You will find a few more similar arguments floating on the web which tell you why typeof NaN is a "number". This blog however, has a very simple logic to justify the typeof iamNaN to be a "number" and not that of whyNotMe.

Consider this:

What would you call an engineer A, who successfully passes his final exams? A passed engineer or may be just an engineer. Now, what you would you call an engineer B, who just failed his exams? Probably a failed engineer, or may be just an engineer. So, both A and B are engineers, and it just happens that A is a passed engineer and B is a failed engineer. iamNaN (not a number) is like B here. It's a failed number. Just as the typeof B is an engineer (even if failed), the type of iamNaN is a number (even if failed)

In contrast a medical student who passes or fails his exams can never be called a failed engineer. This is exactly why typeof whyNotMe (a string) can never be a "number".

You should now be able to digest the fact that typeof NaN is indeed  a "number"!

isNaN(true) // yields false
Can you guess why?

The HTML Attributes Never Change!

First things first! What is an HTML attribute? The html attributes provide additional information about or to the element . For instan...