最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript, calling function - Stack Overflow

matteradmin6PV0评论

from previous help I am using something like this:

(function (global) {

  // your code here

  global.myGlobalVar = myVar

}(this));

which works great for variables, but how do I do it for functions?

For example I tried this:

(function (global) {

  function something()
{
// do something, return something
}

  global.something()= something();

}(this));

but that does not work :(

How do I get it to work with functions?

Thanks!

EDIT:

Please note that this is being called in a html page, first I do this:

<script language="Javascript" src="four.js">

then

<body onload="javascript:something()">

from previous help I am using something like this:

(function (global) {

  // your code here

  global.myGlobalVar = myVar

}(this));

which works great for variables, but how do I do it for functions?

For example I tried this:

(function (global) {

  function something()
{
// do something, return something
}

  global.something()= something();

}(this));

but that does not work :(

How do I get it to work with functions?

Thanks!

EDIT:

Please note that this is being called in a html page, first I do this:

<script language="Javascript" src="four.js">

then

<body onload="javascript:something()">
Share Improve this question edited Jul 11, 2011 at 17:25 Ryan asked Jul 11, 2011 at 17:10 RyanRyan 10.1k23 gold badges68 silver badges103 bronze badges 2
  • 5 You have to realize that functions are just values like anything else. You can refer to them with their name and call them by adding parenthesis after their name. – Felix Kling Commented Jul 11, 2011 at 17:14
  • 2 In onload, you should not write javascript: (it is sometimes used in <a href="">). You just simply write the function name you want to execute. – kapa Commented Jul 11, 2011 at 17:35
Add a ment  | 

3 Answers 3

Reset to default 5

If you want to declare a function, you should not execute it. So remove ().

(function (global) {

  function something()
{
// do something, return something
}

  global.something = something; // something is the variable
                                // containing the function and
                                // you store it into global


}(window));

In Javascript, a function can be stored in a variable (as it is an object basically).

You could do something like this using a closure:

(function (global) {

  global.something= function () {
      // do something, return something
  };

}(this));

Remember, if you write () after a function name, it means you're executing it. If you want to pass the function itself, you simply write its name.

Consider this example:

var x = something(); //x will hold the RETURN value of 'something'
var y = something; //y will hold a reference to the function itself

So after doing the 2nd example, you could do: var x = y(); which will actually give you the same result if you just simply did the 1st example.

(function (global) {

  global.something = function()
  {
    // do something, return something
  }

}(this));

Updated question:

<body onload="javascript:something()">

This won't work. Try this instead:

<body onload="something()">

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far