OOP guys Beware!! A function in Javascript is not what you think
When the below snippet was run, I was shocked and I realized that I should be very careful with JavaScript.
function test(){
console.log(“Hello”)
}var t = test;
t.id=”1";
t();
console.log(t.id);
console.log(typeof(t))
Output:
Hello
1
function
Then i did the research and understood what I have missed while learning JavaScript.
Every function in JavaScript is a Function
object.
A function without a return statement will return a default value.
In the case of a constructor called with the new
keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined
.