最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript, calling function - Stack Overflow

programmeradmin3浏览0评论

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()">
发布评论

评论列表(0)

  1. 暂无评论