Preface
- I know the right code for these examples.
- What I want to know is why the following examples won't work as expected.
Code
With parentheses when calling
sayItfunction.function Fruit(type){ this.type = type; this.taste = "Awful"; this.thought = sayIt(); } function sayIt(){ return this.taste+" "+ this.type; } window.onload = function (){ var lemon= new Fruit("Lemon"); alert(lemon.thought); };This will alert "undefined undefined", why?
sayItfunction without parentheses.function Fruit (type){ this.type = type; this.taste = "Awful"; this.thought = sayIt; } function sayIt(){ return this.taste +" "+ this.type; } window.onload = function (){ var lemon= new Fruit("Lemon"); alert(lemon.thought); };This will literally write down the function on the alert box, why?
Thank you in advance.