Javascript中允许点(方法调用)之前的空格? [英] Whitespace before dot (method call) allowed in Javascript?

查看:15
本文介绍了Javascript中允许点(方法调用)之前的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Javascript 中使用以下语法:

I want to use the following syntax in Javascript:

var bar = foo.replace(/really long regex/, something)
             .replace(/another really long regex/, something)
             .replace(/yet another really long regex/, something);

因为它(在某种程度上)比在一行上执行此操作更具可读性.

since it's (somewhat) more readable than doing this on one line.

这允许吗?它在 Firefox 中运行良好,但我在任何地方都找不到对这种语法的任何引用,而且我不想稍后发现有一些模糊的浏览器无法正常工作.

Is this allowed? It works fine in Firefox, but I can't find any reference to this syntax anywhere, and I don't want to find out later that there's some obsure browser where this doesn't work.

推荐答案

是的,Javascript 忽略空格.

Yes, Javascript ignores whitespace.

尽管如果您要忽略空格,您需要确保并使用分号来结束您的表达式.分号不是必需的,但如果不使用分号,javascript 会使用回车来分隔命令.如果您没有预料到,这可能会导致不一致的行为.

Although if you are going to ignore whitespace you need to be sure and use semicolons to end your expressions. Semicolons are not required, but if they are not used, javascript uses carriage returns to separate commands. Which can cause inconsistent behavior if you're not expecting it.

我在我的 javascript 代码中做了很多次,因为当我编写漂亮的代码时,我不喜欢让它超过 80 个字符.

I have done it many times in my javascript code, since when I'm writing pretty code I don't like to let it go beyond 80 characters.

看看emcascript标准https://docs.google.com/viewer?url=http%3A%2F%2Fwww.ecma-international.org%2Fpublications%2Ffiles%2FECMA-ST%2FECMA-262.pdf

Take a look at the emcascript standard https://docs.google.com/viewer?url=http%3A%2F%2Fwww.ecma-international.org%2Fpublications%2Ffiles%2FECMA-ST%2FECMA-262.pdf

行终止符(这是您所关心的)在 pdf 的第 25 页(第 15 页印在工作表上)中得到了解决.第 7 节的所有内容都与格式有关,您可能会在那里找到它为何起作用的定义(是的,我很懒,不想阅读整个部分 :)).

Line Terminators (which is what your concerned with) are addressed on page 25 of the pdf(page 15 is printed on the sheet). All of section 7 is about formatting, you'll probably find a definition of why it works there(yep I'm lazy and don't want to read the whole section :) ).

希望有帮助!

这篇关于Javascript中允许点(方法调用)之前的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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