-5

When I load an html page, a series of javascript functions are being called despite no reference to them being made. The below functions will alert the user "hello" and " world", even though I do not call validate or donotcall2

<script >
var validate = new function() {
    alert('hello');
};

var donotcall1 = function() {
    alert('hello cruel');
    return false;
};

var donotcall2 = new function() {
    alert(' world');
};
</script>
ppostma1
  • 3,616
  • 1
  • 27
  • 28
  • 1
    i get an error: `Expected '('` for the missing paramter list. – Nina Scholz Jan 04 '18 at 21:59
  • 2
    None of this makes any sense. Did you mis-copy your code into your question? – Kevin B Jan 04 '18 at 22:04
  • I am fixing it now. – ppostma1 Jan 04 '18 at 22:10
  • 1
    after update... it should be alerting 'hello' followed by ' world', but you're indicating it does something else? – Kevin B Jan 04 '18 at 22:11
  • I had a lengthy set of javascript in a page and spent a day trying to sort out why 5 functions were being called. As I was posting the question here a co-worker was testing the code and solved it. When they solved it, rather than posting 140 lines of js here, I reduced the problem down to the basic set to represent the problem. (and mistyped it). I can post the solution as I create the question, but cannot select the answer for 2 days – ppostma1 Jan 04 '18 at 22:15

3 Answers3

1

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");
};
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

You're using incorrect notation. Use the following instead.

var validate = function() {
  alert("Hello");
}

.. etc.

Note the use of var rather than function, and the absence of the new keyword.

gilbert-v
  • 1,263
  • 1
  • 11
  • 23
  • Why the down-vote? I gave an answer that satisfied the OP's needs. My code was used in the selected answer. – gilbert-v Jan 05 '18 at 10:04
-1

The normal way of declaring a variable (which is what you are doing) is:

const donotcall1 = function() {
// if you want to reassign donotcall1 later use let instead of const
    alert('hello cruel');
    return false;
};

(In ES5 and lower you would use var instead of const or let)

Alternatively you can use

function donotcall1 () {
    alert('hello cruel');
    return false;
};

Note the subtle difference.

SourceOverflow
  • 1,960
  • 1
  • 8
  • 22
  • and using the word 'new' before function causes execution of the following function. – ppostma1 Jan 04 '18 at 22:24
  • 1
    whoever downvoted this, could you please explain why? All I wrote is true and addresses and solves the problem – SourceOverflow Jan 04 '18 at 22:34
  • yes, it seems that one cannot post questions or answer here anymore without being spam down-voted. It is community killing to just kick people around without explanation for improvement – ppostma1 Jan 04 '18 at 22:36