可以将 `var` 声明分成多行而不重复使用 `var` 关键字吗?
我的 javascript 函数中有很多变量,不适合一行声明。
var query_string = $('list_form').serialize(), container = $('list_container'),position = container.cumulativeOffset(), layer = $('loaderLayer'),
remote_url = "/web/view/list_offers.php?" + query_string;
有没有一种方法可以仅使用一个 var
关键字在多行上定义这些变量?
I have many variables in my javascript function and don't fit on one line declaration.
var query_string = $('list_form').serialize(), container = $('list_container'),position = container.cumulativeOffset(), layer = $('loaderLayer'),
remote_url = "/web/view/list_offers.php?" + query_string;
Is there a way to define these variables on multiple lines with only one var
keyword?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我没记错的话,这是 Doughlas Crackford 提倡的声明脚本需要的多个变量的模式。
快速提示: 注意JavaScript 提升:)
If I recall it correctly, that is the pattern advocated by Doughlas Crackford to declare multiple variables that script needs.
Quick Tip: Beawere of JavaScript Hoisting :)
您可以用逗号分隔声明:
我不经常看到这种情况,但它是有效的。您还可以在以下位置添加初始值:
您确实不经常看到这种情况,但它又是有效的。我认为它很难阅读,因此也很难维护,所以你最好这样做:
You can seperate declarations by commas:
I don't see this often but it's valid. You can also add the initial values in:
You really don't see this often, but again it's valid. I would argue that it's hard to read, and therfore hard to maintain, so you are better off just doing:
声明变量时不给出任何数据类型,例如 var ,如下所示。
query_string== $('list_form').serialize();
container = $('list_container');
position = container.cumulativeOffset();
layer = $('loaderLayer');
remote_url = "/web/view/list_offers.php?" + query_string;
它会起作用的。
Declare the variables with out giving any datatype like var as below.
query_string== $('list_form').serialize();
container = $('list_container');
position = container.cumulativeOffset();
layer = $('loaderLayer');
remote_url = "/web/view/list_offers.php?" + query_string;
it will work.