Task #4312 (closed)
Opened 8 years ago
Closed 7 years ago
Javascript style coding
| Reported by: | atarkowska | Owned by: | atarkowska |
|---|---|---|---|
| Priority: | minor | Milestone: | Unscheduled |
| Component: | General | Version: | n.a. |
| Keywords: | n.a. | Cc: | |
| Resources: | n.a. | Referenced By: | n.a. |
| References: | n.a. | Remaining Time: | 0.0d |
| Sprint: | n.a. |
Description (last modified by atarkowska)
We should define one common coding style in order to have consistent code and easy readable.
APIDoc:
- JSDoc Toolkit http://code.google.com/p/jsdoc-toolkit/
- YUIDoc http://yuilibrary.com/projects/yuidoc
/**
* Reverse a string
*
* @param {String} input String to reverse
* @return {String} The reversed string
*/
var reverse = function (input) {
// ...
return output;
};
Javascript:
- http://amix.dk/blog/post/19496
- http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
- http://javascript.crockford.com/code.html
- http://www.komodomedia.com/blog/2008/09/javascript-classes-for-n00bs/
- http://www.codeproject.com/kb/aspnet/JsOOP1.aspx
jQuery:
Public
Public functions and variables are available to access on an instance of a class.
function MyClass () {
//..
}
//public member variable
MyClass.prototype.publicVar = "My Public Variable";
//public member function
MyClass.prototype.publicFunction = function () {
alert( this.publicVar );
}
//create an instance
var oClass = new MyClass();
//run a member function
oClass.publicFunction(); //Alert: "My Public Variable"
Private
Private member functions and variables are hidden to outside code. Only public functions can access them.
function MyClass () {
//reference to this
var self = this;
//private member variable
var privateVar = "My Private Variable";
//public member variable
this.publicVar = "My Public Variable";
//private member function
//shorthand function privateFunction() {...}
var privateFunction = function () {
self.publicVar += " Modified By A Private Fucntion";
alert( self.publicVar );
}
}
//create an instance
var oClass = new MyClass();
//run a private member function
oClass.privateFunction(); //private function is undefined
//get a private member var
alert( oClass.privateVar ); //private var is undefined
Privileged
A privileged member function has access to private variables, but is available publicly.
function MyClass () {
//private member variable
var privateVar = "My Private Variable";
//privileged member function
this.privilegedFunction = function () {
alert( privateVar );
}
}
//create an instance
var oClass = new MyClass();
//run a privileged member function
oClass.privilegedFunction(); //Output: alerts the value of the private var
Static
A static function or variable is available on the base class (or JavaScript?) function, but is not available to the class instance.
function MyClass () {
//...
}
//declare a static member
MyClass.staticVar = "My static variable";
//declare a static function
MyClass.staticFunction = function ( pInput ) {
return new MyClass( MyClass.staticVar , pInput );
}
//create an instance
var oClass = new MyClass();
//run a static function (NO access to private or public)
oClass.staticFunction( 9 ); //staticFunction is undefined on an instance
//run a privileged member function on the class
MyClass.privilegedFunction(); //The function runs
Change History (17)
comment:1 Changed 8 years ago by atarkowska
- Status changed from new to accepted
comment:2 Changed 8 years ago by atarkowska
- Description modified (diff)
comment:3 Changed 8 years ago by atarkowska
- Description modified (diff)
comment:4 Changed 8 years ago by atarkowska
- Description modified (diff)
comment:5 Changed 8 years ago by atarkowska
- Description modified (diff)
comment:6 Changed 8 years ago by atarkowska
- Remaining Time changed from 2 to 1.5
comment:7 Changed 8 years ago by jburel
- Sprint changed from 2011-02-10 (5) to 2011-02-24 (6)
comment:8 Changed 8 years ago by atarkowska
- Owner atarkowska deleted
- Status changed from accepted to new
comment:9 Changed 8 years ago by atarkowska
- Owner set to atarkowska
comment:10 Changed 8 years ago by atarkowska
- Remaining Time changed from 1.5 to 1
comment:11 Changed 8 years ago by atarkowska
- Sprint changed from 2011-02-24 (6) to 2011-03-10 (7)
comment:12 Changed 8 years ago by atarkowska
- Sprint changed from 2011-03-10 (7) to 2011-03-24 (8)
comment:13 Changed 8 years ago by atarkowska
- Sprint changed from 2011-03-24 (8) to 2011-04-07 (9)
comment:14 Changed 8 years ago by atarkowska
- Sprint changed from 2011-04-07 (9) to 2011-04-21 (10)
comment:15 Changed 8 years ago by atarkowska
- Milestone changed from OMERO-Beta4.3 to Unscheduled
- Sprint 2011-04-21 (10) deleted
comment:16 Changed 8 years ago by wmoore
Referencing ticket #3300 has changed sprint.
comment:17 Changed 7 years ago by wmoore
- Remaining Time changed from 1 to 0
- Resolution set to fixed
- Status changed from new to closed
I vote for using Crockford's style, since we are now using jsHint.
Moved from sprint 2011-02-10 (5)