当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js 匿名调用实现代码

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 js 匿名调用实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 326 ::
收藏到网摘: n/a

js中定义函数的方式有多种,函数直接量就是其中一种。如var fun = function(){},这里function如果不赋值给fun那么它就是一个匿名函数。 好,看看匿名函数的如何被调用。
1、执行后得到返回值的函数调用
Js代码
复制代码 代码如下:

//方式1,调用函数,得到返回值。强制运算符使函数调用执行
(function(x,y){
alert(x+y);
return x+y;
}(3,4));

Js代码
//方式二,调用函数,得到返回值。强制函数直接量执行再返回一个引用,引用再去调用执行
复制代码 代码如下:

(function(x,y){
alert(x+y);
return x+y;
})(3,4);


2、执行后忽略返回值
Js代码
复制代码 代码如下:

//方式三,调用函数,忽略返回值
void function(x) {
x = x-1;
alert(x);
}(9);

//方式三,调用函数,忽略返回值
复制代码 代码如下:

void function(x) {
x = x-1;
alert(x);
}(9);

嗯,最后看看错误的调用方式
Js代码
//错误的调用方式
复制代码 代码如下:

function(x,y){
alert(x+y);
return x+y;
}(3,4);