开发者

Coding standards and best programming practices for JavaScript [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am looking for some decent coding standards and best programming practices for JavaScript?

I have a C# background. How are controls named on the web page? If you have a button, how would you name the id property? In AS开发者_JS百科P.NET we would call it something like btnSearch. Treeviews would be something like tvUsers.

I am using the YUI API, but the same would apply here.


I have a C# background. How are controls named on the web page?

The client id is a property of the control: controlId.ClientID

Example:

string MyJavaScriptControlId = btnSearch.ClientID;

Or, in your aspx:

<script type="text/javascript">
    htmlElement = document.getElementById('<% tvUsers.ClientID %>');
</script>


There is no definite style guide like PEP-8 for JS. JavaScript Garden contains some good information on its quirks, though. You'll likely learn a trick or two by studying it. :)

Considering your id question I rather tend to avoid ids when writing applications. This is simply due to the fact that it's way too easy to end up with clashes.

Instead I like construct my elements dynamically via JS (direct ref to elem) and to use class attributes for styling (ie. ".clear.button"). I'm not sure if anyone else does it this way, though...

Since you are dealing with YUI it's likely you have some specific conventions there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜