Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These are just a few quick guidelines I've come up with, if anyone else has anything meaningful to add, I've set this answer as a community wiki so it should be easy enough for you to edit.</p> <ol><li> Namespace your objects to ensure they will never conflict with third party JavaScript libraries. <pre>window['Andrew']['JS'] = { addEvent: function(el,evName) {/*Stuff*/}, Rectangle: function(width,height) {/*Stuff*/} };</pre> So then you would create a rectangle object by using: <pre>var myRect = new Andrew.JS.Rectangle(14,11);</pre> And then your code will never interfere with, or be interfered by anybody else's <code>Rectangle</code>.<br /><br /> </li> <li>Use a consistent naming strategy, specifically: <ul> <li>Object names should be capitalized, everything else (variables, functions) should begin with a lower case character i.e. <pre>var myRect = new Andrew.JS.Rectangle(14,11); document.write(myRect.getArea());</pre> </li> <li>Ensure everything is meaningful, i.e. verbs for methods, nouns + adjectives for parameters.</li> </ul><p></p> </li> <li>Make sure all methods and parameters are <b>relevant</b> to the object they belong to. e.g. In this example, the area of the rectangle can be converted to square feet using the method <code>inSquareFeet()</code>.<p></p> <pre>myRect.getAreaObject().inSquareFeet();</pre> Make sure inSquareFeet is a method of the object returned by <code>getAreaObject()</code> and not a method of <code>Andrew.JS.Rectangle</code> <p></p> </li> <li>Use constructors, or more specifically, try as hard as possible to make sure that an object doesn't need any further initialization to be used once it has been constructed, so instead of: <pre>var Person = function() { this.name = ""; this.sayHello = function () { alert(this.name + " says 'Hello!'"); return this; } } var bob = new Person(); bob.name = "Bob Poulton"; bob.sayHello();</pre> try: <pre>var Person = function(name) { this.name = name; this.sayHello = function () { alert(this.name + " says 'Hello!'"); return this; } } var bob = new Person("Bob Poulton"); bob.sayHello();</pre> </li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload