风之石 function(){alert(1); }();//不加括号也可以?.<html>
<body>
<script>
c=function(){alert(1); }();//不加括号也可以?
</script>
</body>
</html>
.
c=(function(){alert(1); })();
枯井的理解:11楼
c=function(){alert(1); };//相当于定义一个函数c 通过调用c(); 来执行alert
-------------
c=function(){alert(1); }();//相当于定义函数C 并调用他
等同于
c=function(){alert(1); };
c();
非哥的理解:17楼恩, 名称为B 的函数 这句话是错误的
----------------------------------
从头说function(){}();是错误的,原因是 匿名函数不能这样执行.(function(){})();这是正确的。B=function(){}();与B=(function(){})();B 都是匿名函数执行后的返回值
<body>
<script>
c=function(){alert(1); }();//不加括号也可以?
</script>
</body>
</html>
.
c=(function(){alert(1); })();
枯井的理解:11楼
c=function(){alert(1); };//相当于定义一个函数c 通过调用c(); 来执行alert
-------------
c=function(){alert(1); }();//相当于定义函数C 并调用他
等同于
c=function(){alert(1); };
c();
非哥的理解:17楼恩, 名称为B 的函数 这句话是错误的
----------------------------------
从头说function(){}();是错误的,原因是 匿名函数不能这样执行.(function(){})();这是正确的。B=function(){}();与B=(function(){})();B 都是匿名函数执行后的返回值
解决方案 »
- asp.net c# 只显示日期,不显示时间
- css的简单问题
- 高手帮我 关于个性化属性的问题
- VS2005下,frame框架取Cookies问题
- 为什么我调用web services后,过一段时间再调用,连接就会被关闭。
- 这个问题我怎么也搞不清楚~~~
- 请问:请问如何在本地的UDDI(http://localhost//uddi)中注册一个Web服务?谁能举例描述一下基本步骤,谢谢!
- mvc Repository 个人感觉比较麻烦
- viewstate 中保存我动态生成的Table控件,提示错误让我序列化,请教
- 那里写错了,怎么不能排序了??? 急 !在线等待!!!!!!!
- Windows文件夹操作!
- 自定义控件 布尔属性赋值
要不要我去web版下个赌注,大家买谁赢?
我同样关心你在WEB版押谁不会变成枯井吧
(还是说我有些落伍?不过什么都追最新的... ... 会累死,产出也不咋地)
等号的优先级高于括号
c=function(){alert(1); }();
=>运行函数表达式返回x相当于:x=function(){alert(1); }
=>按优先级会先运行x即:x()
=>运行赋值运算,由于函数没有返回值所以:c=undefined下一个问题是没有c=的时候为什么
function(){alert(1); }()
这个错误
(function(){alert(1); })()
这个又可以运行呢
因为js是一种会自动判断结尾的语言
当一个function以这个形式出现
function(){}
而前面又没有运算符号时会看作是一个函数定义语句
那么function(){alert(1); }()
其实是
function(){alert(1); };
();
这里的();会造成语法错误所以直接会报错
如果前面有运算符时那么这个函数会看成是一个函数表达式
(function(){alert(1); })()
相当于运行function获得返回值(函数的引用)在进行()运算
跟第一个例子一样
c=function(){alert(1); }(); function(){alert(1); }优先级>()>=研究这位仁兄的代码,这位仁兄的解答很是透彻! function(){alert(1); }您用换原的思想解释的 生动活泼
改为 function(){alert(1); return "asdsad";}();再分别执行
c=(function(){alert(1); return "asdsad"})();
alert(c);
和
c=function(){alert(1); return "asdsad"};
c();
alert(c);
所得到结果是不一样的
相当于定义function获得函数的引用(表达式的返回值)再进行()运算这样比较对
=>
function(){alert(1); }()
=>
c=function(){alert(1); }() function(){alert(1); }优先级高于()优先级高于=
谁能打开这个网址,帮我把内容贴一下,多谢~
http://www.hedgerwow.com/360/dhtml/js-anonymous-function-patterns.html
不是 function(){alert(1); }优先级高于() 是 function(){alert(1); }() 优先级高于 = = 的优先级原因 让
function(){alert(1); }()
变成了 (function(){alert(1);}( ));
<script>
function(){
alert(1);
}();
</script>Pattern 1 : Function Literal
Declare Function as an Object first then execute it.<script>
(function(){
alert(1);
} ) ( );
</script>
Pattern 2 : Prior Expression
Use parentheses to force a declared function to be executed since JavaScript evaluates expressions from the inner parentheses to outer parentheses<script>
( function(){
alert(2);
} ( ) );
</script>Pattern 3 : Void Operator
We can use "void" to evaluate a lone operand without using parentheses as the wrapper. of function<script>
void function(){
alert(3);
}()
</script>