;( function( ) {} );

JavaScript Semicolon Insertion

Everything you need to know

jquery 库和其他javascript代码里经常看到函数前面有分号,加号或者其他操作符号。

在函数前面加:+  ;  ~ !  – 等等一元操作符,javascript 引擎都会将后面的statement转换成表达式(expression),这样就可以调用了。

一、分号

加分号除了自调用的目的外,还有另外一个目的。当两个js文件压缩在一起的时候,为了防止上一个文件没有以分号结束,如果那也的话就会引起错误:

A.js文件的内容:

1
2
function add (a,b){return a+b}
document.write(‘hello world’)

B.js文件的内容:document.write(‘hello javascript’);

合并压缩后的文件:document.write(‘hello world’)document.write(‘hello javascript’);

这样就会发生语法错误。当然这样简单的js代码很少会分成两个文件来写。但现实世界,说能说的清楚。

所以在函数前加分号又多了一层意思:防止多个.js文件压缩合并后产生语法冲突。

二、加号

在javascript里,加号有转换类型的作用,会将后面的值转换成number,所以当执行:console.log(typeof +function(){});

会得到结果number

其实最好的办法是加括号

(function(){console.log(‘hello world’)})();

javascript 函数之——函数前面的一元操作符(unary)

via http://html5lin.com/javascript-function-and-unary/

javascript中为何在匿名function函数后面还外加一个括号

(function(){…}())或 (function(){})()

匿名函数自调用”,也就是说,定义一个匿名函数,然后马上调用它(因为它是匿名的,如果不立即调用就获取不到该函数的引用了)。

通常它被应用在一些大型的JS框架中(如上面所说的),因为这个匿名函数的函数体相当于提供一个匿名的名字空间,这样就不会再与用户自定义的JS函数、变量、对象发生冲突了。尽管JS没有显示地提供命名空间的定义和使用机制,但这种匿名方式却不失为是一种很好的解决命名空间问题的方法。

(function(){代码})()就等于执行了一个函数,只不过它是匿名的而已。如果在这个匿名函数内部想再次调用这个函数,就需要调用constructor属性了(这是Object中定义的,JS的继承机制如同Java一样保证了了所有对象都继承Object类)。

1、下列哪些正确?(B、C

A.function(){

       alert(“Here!”);

    }();

B.(function(){

       alert(“Here!”);

    })();

C.(function(){

       alert(“Here!”);

    }());

2、下列哪个结果是正确的?(A、B、C、D

A.(function(a1,a2){

       alert(“Here!”+(a1+a2));

    })(1,2);

B.(function(a1,a2){

       alert(“Here!” +(a1+a2));

    }(1,2));

C.void function(a1,a2){

       alert(“Here!” +(a1+a2));

    }(1,2);

D.var f = function(a1,a2){

       alert(“Here!” +(a1+a2));

    }(1,2); 

注:A 、B、C与D四种格式都正确,前两者属于同种情况的不同写法,后两种是将函数对象的返回值赋给其他变量,C是忽略函数返回值,而D正相反!

空语句。 一个分号,只有一个分号的时候,它是一个空语句,并且什么也不干。 空语句可以出现在任何一个语句可以出现的地方。

有些情况当需要一个语句时,空语句是很有用的,但不是必须的。 这种情况下,块代码也是允许的,只是一个空的块比一个分号要长而已。

例如: 下面2个语句是等效的

while(processNextItem() > 0);
while(processNextItem() > 0) {}
人不是机器,总有可能忘记写分号。上面的分析说明,对于人来说,行首特例加分号的策略其实更简单易行。
Advertisements

8 responses to “;( function( ) {} );

  1. Now I am going away to do my breakfast, after having my
    breakfast coming over again to read additional news.

  2. What’s Going down i am new to this, I stumbled upon this I have discovered It positively helpful and it has helped me out loads. I’m hoping to
    give a contribution & help other customers
    like its aided me. Great job.

  3. This is my first time go to see at here and i am actually
    impressed to read everthing at single place.

  4. Hello there! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me.
    Anyways, I’m definitely delighted I found it and I’ll be book-marking and
    checking back often!

  5. Your mode of telling the whole thing in this paragraph is actually nice,
    every one be able to easily know it, Thanks
    a lot.

  6. Hey, I think your website might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you
    a quick heads up! Other then that, wonderful blog!

  7. Wow, that’s what I was searching for, what a data! existing here at this webpage, thanks admin of this web page.

  8. Great information. Lucky me I recently found your blog by
    chance (stumbleupon). I’ve bookmarked it for later!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: