+ 1
Why doesn't this code work ?
https://code.sololearn.com/W0AeMqk3V08L My expectation is the code prints "text" It gives an error of onload.type() function is not defined. Can someone please explain why doesn't this code just work? Thank you
2 Answers
+ 2
You are trying to create an object which means you need the new keyword and brackets after it like so:
new onload()
Then the function:
new onload().type();
How you defined a method is incorrect, you don't use the name of the prototype but the "this" keyword because the prototype is not fully defined yet like so:
this.type = type;
Which can be shortened to:
this.type = function() {//code};
Or even:
this.type = () => {//code};
Changes:
https://code.sololearn.com/WjjQdKhZEYna/?ref=app
+ 3
https://code.sololearn.com/WrJd7H9QY39h/?ref=app