1

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()">
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. 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. Commented Jul 11, 2011 at 17:35

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

7 Comments

Note that the global trick used here won't work in ES5 strict mode.
@Reid: I'm not aware of that - could you please elaborate on that?
In this code, this refers to the global object (window). In strict mode, that's no longer the case: this when used like this will give undefined instead. If this anonymous function was executed in the context of another function, it would work, but this will never refer to the global object in strict mode (unless specifically bound).
For comparison: (function () { return this; }()); returns window, while (function () { 'use strict'; return this; }()); returns undefined.
Hi! I usually add the "use strict"; because i want to catch any variables that leak out and as you said I am getting a "undefined" error... how do i correct that?
|
4

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.

1 Comment

Hi, I edited my post, please look above. I am getting 2 errors: "global is undefined" (.js file) and "something is not defined" (html file)
1
(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()">

3 Comments

Hi, I edited my post, please look above. I am getting 2 errors: "global is undefined" (.js file) and "something is not defined" (html file)
If global isn't defined, then your first code block that you said is working, would not work. So what is the code you had that actually works?
@Justin: Your global parameter is only available inside the function, so won't be available to the inline onload. Because you're assigning something to the global object, it should be available from the global window. <body onload="something()">

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.