English 中文(简体)
Defining and calling function in one step
原标题:

Is there a way in Javascript to define a function and immediately call it, in a way that allows it to be reused?

I know you can do one-off anonymous functions:

(function(i) {
    var product = i * i;
    console.log(product);
    // Can t recurse here because there s no (ECMA standard) way for the 
    // function to refer to itself
}(2)); // logs 4

Or you can name a function then call it afterwards:

function powers(i) {
    var product = i * i;
    console.log(i * i);
    if (product < 1e6) { powers(product) };
}

powers(2); // Logs 4, 16, 256...

But is there a cleaner way of defining and calling a function in one go? Sort of like a hybrid of both examples?

Not being able to do this isn t preventing me from doing anything, but it feels like it would be a nice expressive way to write recursive functions or functions that need to be run on $(document).ready() but also later when situations change, etc.

最佳回答

You can try:

(window.powers = function(i) {
  /*Code here*/
  alert( test :   + i);
})(2);
<a href="#" onclick="powers(654)">Click</a>

Working link : http://jsfiddle.net/SqBp8/

It gets called on load, and I have added it to an anchor tag to change the parameter and alert.

问题回答

If all you want is access the function within its own body, you can simply specify a name after the function keyword:

> (function fac (n) {
    return (n === 0 ? 1 : n*fac(n-1));
  })(10)
3628800

This is a standard feature (see ECMA-262, ed. 5.1, p. 98).

All the answers here are close to what you want, but have a few problems (adding it to the global scope, not actually calling it, etc). This combines a few examples on this page (although it unfortunately requires you to remember arguments.callee):

var test = (function() {
  alert( hi );
  return arguments.callee;
})();

Later, you can call it:

test();

If you don t care about the return value, you can do this.

var powers = function powers(i) {
    var product = i * i;
    console.log(i * i);
    if (product < 1e6) { powers(product) };
    return powers;
}(2);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签