Take a look at this JavaScript code. When running my webpage, it says:
TypeError: Dictionary is not a Constructor.
However, when I copy and paste the same thing at Firefox console, it works flawlessly. What's the matter with it?
Take a look at this JavaScript code. When running my webpage, it says:
TypeError: Dictionary is not a Constructor.
However, when I copy and paste the same thing at Firefox console, it works flawlessly. What's the matter with it?
Is it because hoisting?
var dict = new Dictionary();
var Dictionary = function () { ... };
is just like
var Dictionary;
var dict = new Dictionary();
Dictionary = function () { ... };
So when you new
, variable Dictionary
is undefined
.
var dict = new Dictionary()
This part of the code must be placed AFTER the definition of Dictionary, otherwise it obviously fails. It works in your console because the console will run after the object has been defined, whereas in your code you are trying to instanciated a class that does not exist yet.