In the code below I firstly try to print the ‘normal’ way. Secondly I try to anonymize my function and assign it to a variable which I then print. In Chrome this now proceeds to print the source code. What am I doing wrong?
function sumSq() {
var sum = 0;
for (i=0;i<=10;i++) {
sum+=i*i;
}
return sum;
}
console.log(sumSq());
var mySum = function() {
var sum = 0;
for (i=0;i<=10;i++) {
sum+=i*i;
}
return sum;
}
console.log(mySum);
Call
mySumusing():Functions are objects, so when you call
JS calls toString on the mySum object (which mySum inherits from the Object prototype). That’s why the source gets printed.