Javascript代码样式 [英] Javascript Code Styling

查看:46
本文介绍了Javascript代码样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始做更多数量的javascript编码,并认为这可能是调查代码样式的好时机。我主要使用Java开发

,目前使用代码造型技术拼写为

Java。


例如,在我的代码块({})我总是将起始括号

放在与第一行代码相同的行上。例如:


函数myFunction {


但我也注意到很多开发人员将它移到下一行:


函数myFunction

{


我也在每一行的末尾放了一个分号,虽然我看到它' 's
也不是强制性的。


三(以及最后)我还声明所有对象的首字母大写

和所有成员变量/函数小写。例如:


函数MyObject(){


var myFirstMemberVar;


函数doSomething( ){

....

}

}


是否有编码标准JavaScript的?我目前的做法是否可行?b / b
提前付款。

解决方案




Tom Cole写道:


我开始做更多的数量javascript编码并认为这个

可能是调查代码样式的好时机。我主要使用Java开发

,目前使用为
Java编写的代码样式技术。



几年前,当我们(我在西门子工作)开始处理我们的

当前wep应用程序时,我们决定编写我们的自己的JavaScript编码

指南。大多数情况下,它们都受到我们的C / C ++编码指南的启发,并且也受到了Java指南的启发(因为在线发现的代码大多遵循

用于大写的Java指南)。 br />


例如,在我的代码块({})中,我总是将起始括号

放在同一行上第一行代码。例如:


函数myFunction {


但我也注意到很多开发人员将它移到下一行:


函数myFunction

{



这主要是品味问题。我们的指南允许两个但是

推荐第二个,我们发现它更具可读性。如果你使用Visual Studio 2003/2005工作

,它很容易配置


我还放了一个分号在每一行的末尾,虽然我看到它'

也不是强制性的。



JavaScript中不是强制性的,但我们非常推荐。我们在指南中强制要求
。如果你使用像JsMin这样的代码最小化器,

在一行上放置多个语句,那么'';''无论如何都是强制性的。




第三个(也是最后一个)我还用大写字母表示所有对象的大写字母大写

和所有成员变量/函数。例如:


函数MyObject(){


var myFirstMemberVar;


函数doSomething( ){

....

}

}



这是我们也推荐。但是,我们还建议使用匈牙利语

表示变量。这违反了我们的C#指南,但有一个原因:在JavaScript中,对象不是强类型的。因此,他们可能首先包含一个类型,然后是另一个类型。但是,为了避免混淆,我们的指南规定必须避免这种情况,并且

变量必须在其整个生命周期中携带单一类型。为了方便

这个,使用匈牙利表示法,例如strMessage(字符串),

iValue(整数),fValue(浮点数)等...注意

JavaScript没有区分整数和浮点数,所以帮助开发人员真的只需要




通常,JavaScript编码器遵循Java命名准则,就像你一样。


是否有Javascript的编码标准?我目前的做法是否可行?b / b
提前付款。



是的,它们看起来很合理。


HTH,

Laurent

-

Laurent Bugnion,GalaSoft

软件工程: http://www.galasoft-LB.ch

PhotoAlbum: http://www.galasoft-LB.ch/pictures

支持加尔各答的儿童: http://www.calcutta-espoir.ch


Laurent Bugnion写道:

< snip>


>例如,在我的代码块({})中,我始终将
起始括号放在同一块上作为第一行代码。
例如:

函数myFunction {

但我也注意到很多开发人员将它移到<下一行:

功能myFunction
{



这主要是品味问题。我们的指南允许

但推荐第二个,我们发现它更具可读性。



< snip>


我感觉这两个中的一个被发现更具可读性/>
个人是他们更熟悉的人。我发现

前者更具可读性。但我怀疑最重要的是

是一致的,只使用其中一个(当然在任何一个单独的b $ b项目中,可能在任何一家公司内)。


一般来说,JavaScript编码器遵循Java命名

指南,就像你一样。



我当然这样做。


>是否有编码Javascript的标准?我目前的做法是否可行?

提前致谢。



是的,它们看起来很合理。



绝对。


Richard。




Laurent Bugnion写道:





Tom Cole写道:


但我也注意到很多开发人员将它移到下一行:

函数myFunction

{



这主要是品味问题。我们的指南允许两个但是

推荐第二个,我们发现它更具可读性。



我们做了很多JS,我们也喜欢使用下一行。旧的方式

使用相同的线路无疑与早期的终端/内存相关

问题。


[...]使用匈牙利表示法,例如strMessage(字符串),

iValue(整数),fValue(浮点数)等...注意

JavaScript没有区分整数和浮点数,所以它实际上只是帮助开发人员获得



我们使用这些,加上对象的oReference。


干杯,Kev


I''m starting to do more quantity of javascript coding and thought this
might be a good time to investigate code styling. I primarily develop
in Java and currently use the code styling techniques spelled out for
Java.

For example, in my code blocks ( {} ) I always place the starting brace
on the same line as the first line of code. For example:

function myFunction {

But I''ve also noticed a lot of developers move it to the next line:

function myFunction
{

I also place a semicolon at the end of each line, although I see it''s
also not mandatory.

Third (and lastly) I also declare all Objects in first letter uppercase
and all member variables/functions in lowercase. For example:

function MyObject() {

var myFirstMemberVar;

function doSomething() {
....
}
}

Is there a coding standard for Javascript? Do my current practices seem
feasible?

Thanks in advance.

解决方案

Hi,

Tom Cole wrote:

I''m starting to do more quantity of javascript coding and thought this
might be a good time to investigate code styling. I primarily develop
in Java and currently use the code styling techniques spelled out for
Java.

A few years ago, when we (I work at Siemens) started working on our
current wep application, we decided to write our own JavaScript coding
guidelines. Mostly, they are inspired of our C/C++ coding guidelines and
also of Java guidelines (because code found online mostly follows the
Java guidelines for capitalization for example).

For example, in my code blocks ( {} ) I always place the starting brace
on the same line as the first line of code. For example:

function myFunction {

But I''ve also noticed a lot of developers move it to the next line:

function myFunction
{

This is mostly a matter of taste. Our guidelines allow both but
recommend the second one, which we find is more readable. If you work
with Visual Studio 2003/2005, it''s easy to configure

I also place a semicolon at the end of each line, although I see it''s
also not mandatory.

Not mandatory in JavaScript, but it''s very recommended. We made it
mandatory in our guidelines. If you use a code minimizer like JsMin,
placing more than one statement on one single line, then the '';'' is
mandatory anyway.

Third (and lastly) I also declare all Objects in first letter uppercase
and all member variables/functions in lowercase. For example:

function MyObject() {

var myFirstMemberVar;

function doSomething() {
....
}
}

This is also what we recommend. However, we also recommend hungarian
notation for variables. This goes against our C# guidelines, but has a
reason: In JavaScript, objects are not strongly typed. Thus they may
contain a type first, and then another type. However, to avoid
confusion, our guidelines specify that this must be avoided, and a
variable must carry a single type all along its life. To facilitate
this, hungarian notation is used, for example strMessage (string),
iValue (integer numbers), fValue (float numbers), etc... Note that
JavaScript doesn''t differentiate integer and float, so it''s really just
to help the developer.

Generally, JavaScript coders follow the Java naming guidelines, like you.

Is there a coding standard for Javascript? Do my current practices seem
feasible?

Thanks in advance.

Yes, they seem very reasonable.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


Laurent Bugnion wrote:
<snip>

>For example, in my code blocks ( {} ) I always place the
starting brace on the same line as the first line of code.
For example:

function myFunction {

But I''ve also noticed a lot of developers move it to the
next line:

function myFunction
{


This is mostly a matter of taste. Our guidelines allow both
but recommend the second one, which we find is more readable.

<snip>

I have a feeling that the one of the two that is found more readable by
individuals is the one with which they are more familiar. I find the
former more readable. But I suspect that the most important thing is to
be consistent and use only one or the other (certainly within any single
project, and probably within any single company).

Generally, JavaScript coders follow the Java naming
guidelines, like you.

I certainly do.

>Is there a coding standard for Javascript? Do my current
practices seem feasible?

Thanks in advance.


Yes, they seem very reasonable.

Absolutely.

Richard.



Laurent Bugnion wrote:

Hi,

Tom Cole wrote:

But I''ve also noticed a lot of developers move it to the next line:
function myFunction
{


This is mostly a matter of taste. Our guidelines allow both but
recommend the second one, which we find is more readable.

We do a lot of JS, and we prefer using the next line also. The old way
of using the same line is no doubt related to early terminal / memory
problems.

[...] hungarian notation is used, for example strMessage (string),
iValue (integer numbers), fValue (float numbers), etc... Note that
JavaScript doesn''t differentiate integer and float, so it''s really just
to help the developer.

We use these, plus oReference for objects.

Cheers , Kev


这篇关于Javascript代码样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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