Javascript coding guidelines & conventions

Discussion in 'Javascript' started by, May 22, 2005.

  1. Guest


    Found these coding guidelines for C#, HTML, Javascript, Java, HTML,
    PL/SQL, T-SQL, VB and VBScript.

    Well written and free to download.

    , May 22, 2005
    1. Advertisements

  2. wrote:

    > Found these coding guidelines for C#, HTML, Javascript, Java, HTML,
    > PL/SQL, T-SQL, VB and VBScript.
    > Well written [...]

    But still not good enough, e.g.:

    | 2.4.5 Naming conventions
    | [...]
    | JavaScript supports the encapsulation of functions into classes.

    Wrong. JavaScript before version 2.0 is an object-oriented programming
    language that uses only _prototype-based inheritance_. Functions make
    up Function objects that serve as constructors for objects when used as
    operand of the `new' operator:

    function Foo(x)
    this.y = x;

    var f = new Foo(42);

    In JavaScript 2.0 which also provides class-based inheritance, but of
    which currently only available a test implementation codenamed Epimetheus
    is available, the `class' keyword is used to define classes, not the
    `function' keyword:


    | Our standard is to prefix all member function definitions with
    | the class name followed by an underscore as the following example
    | will make clear:
    | // Class definition, full comments omitted for clarity
    | function CProducts
    SyntaxError: missing ( before formal parameters

    | {
    | // ** Methods
    | this.render = CProducts_Render;
    | this.load = CProducts_Load;
    | }
    | function CProducts_Render()
    | {
    | ...
    | }

    This way of implementing methods counteracts proper object-oriented
    programming style as the methods also become available as global methods.
    Sometimes this is intended (e.g. for the purpose of backwards compatibility
    to previous versions of a library that did not use OOP properly), however
    here it obviously is not. Thus it is much better to write

    function CProducts()
    // ...

    CProducts.prototype = {
    render: function CProducts_Render()

    // ...

    here, or, if the prototype of CProducts should extend another prototype:

    CProducts.prototype.render = function CProducts_Render()

    | [...]
    | 2.4.10 Including files
    | Including JavaScript files is easy as this is supported by HTML.
    | It will be no surprise that the syntax is:
    | <SCRIPT LANGUAGE="JavaScript" SRC="javascript/library.js"></SCRIPT>

    But it should be in fact a surprise to find that in a guideline for
    commercial scripts since it is from deprecated to wrong. New projects
    should use at least HTML 4.01 and in HTML 4 (and XHTML) the `type'
    attribute is required for the `script' element while the `language'
    attribute is deprecated (Transitional) or not even valid (Strict).

    | 2.4.11 Clean up your objects
    | Even though JavaScript has a built in garbage collector to reclaim
    | resources from un-references objects and variables, it is good practive
    | destroy your objects as soon as possible. This will result in a lower
    | memory footprint in the customer's browser.
    | The JavaScript syntax to dereference an object is
    | objectName = null;

    Wrong. JavaScript and Java have garbage collection for a good reason, that
    is, an object may be referenced by more than one identifier. Assigning
    `null' to an variable that previously referenced an object neither in Java
    nor in ECMAScript, JScript or JavaScript forces the garbage collector to
    destroy the referenced object; it is only destroyed if there is no further
    reference to it *and* the garbage collection algorithm decides that it
    should be destroyed. So using that statement does not reliably reduce the
    memory footprint, instead it wastes computing time for the unnecessary

    | 2.4.12 Reading & writing configuration settings
    | JavaScript in the browser can not directly access either the registry or a
    | configuration database, therefore configuration settings need to be passed
    | by JavaScript using ASP. [...]

    Wrong. Even JScript in ASP can only access databases using a server-side
    API, that is, in most cases, but not restricted to, that of the Internet
    Information Server. So it is rather a question whether a client-side API
    for the described processes is available which in turn depends on the
    environment client-side scripts run; e.g. if running within a HyperText
    Application (HTA), it is entirely possible to access the Registry via
    objects of Windows Script Host using client-side JScript; the best example
    for this is probably the Windows NT 5.0 (2k/XP) Control Panel where e.g.
    the "Add/Remove Application" dialog is based on an HTA.

    Furthermore, ASP is definitely not required server-side, there are
    other platforms that enable one to perform the described processes.

    I second many other recommendations of the Demachina Coding conventions &
    guidelines (such as the indentation style), however it has to be pointed
    out that it in its current form cannot be recommended as viable reference

    Thomas 'PointedEars' Lahn, May 28, 2005
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francisco Camarero

    VHDL Coding Guidelines

    Francisco Camarero, Jul 8, 2003, in forum: VHDL
  2. Roger

    Portable Coding Guidelines?

    Roger, Dec 17, 2004, in forum: VHDL
    Dec 17, 2004
  3. Replies:
    Lasse Reichstein Nielsen
    May 22, 2005
  4. Otto Wyss
    Otto Wyss
    Jun 8, 2004
  5. Stuart Gerchick
    Steven T. Hatton
    Nov 12, 2004
  6. Aravind

    security coding guidelines for C/C++

    Aravind, May 24, 2004, in forum: C Programming
    Michael Wojcik
    May 28, 2004
  7. arnuld

    C coding guidelines

    arnuld, Aug 26, 2009, in forum: C Programming
    Sep 16, 2009
  8. Replies: