为什么在定义函数之前需要使用分号? [英] Why need to use semicolon before defining a function?

查看:301
本文介绍了为什么在定义函数之前需要使用分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些jQuery插件源代码的函数开头看到了一些奇怪的; ,如下所示:

 ;(function($){..... 

有人可以解释为什么他们需要在这种情况下使用;

解决方案

当此文件中当前存在的代码最后不包含; 时,此分号将帮助您将新代码正确地连接到文件中。 p>

例如:

 (function(){

})()//< ---没有分号

//添加分号以防止先前代码产生意外的懒惰
;(function($){

})();

没有分号,第二个()将被解释为函数调用,并将尝试调用第一个函数的返回结果


I've seen some strange ; at the beginning of a function in some jQuery plugins source code like this:

;(function ($) {.....

Can someone explain why they need to use ; in this case?

解决方案

This semicolon will help you to properly concatenate a new code into a file when the current existed code in this file does not include a ; at the end.

For example:

(function() {

})()  // <--- No semicolon

//  Added semicolon to prevent unexpected laziness result from previous code    
;(function ($) {

})();

Without the semicolon, the second () would have been interpreted as a function call, and will tried to call the return result of the first function

这篇关于为什么在定义函数之前需要使用分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆