The problem is that you’re using new
. When you write:
variable = new <something>;
it means to call the function <something>
as an object constructor, e.g.
var myObj = new Array;
calls Array()
as a constructor, and the returned object is assigned to myObj
.
While this is normally done using named functions, it works exactly the same with anonymous functions, and that’s what your code is doing. It’s defining an anonymous function, then calling it as an object constructor, and assigning the returned object to the variable validate
.
To assign the function itself to the variable, don’t use new
.
var validate = function() {
alert("Hello");
};
solved Javascript is executing (all) functions when they are declared