Coding Patterns"

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

(New page: This page lists some interesting coding patterns that we may or may not use in our code. == Self Executing Functions == This is already a classic. Using a self calling function declarati...)
 
 
Line 45: Line 45:
 
         ...  
 
         ...  
 
}  
 
}  
</pre>
 
 
== Constructors as Pure Functions ==
 
 
On some specific cases, to avoid confusion, there would be some advantage on having "classes" constructors behave like normal functions. To do that:
 
 
<pre>
 
function FCKeditor( instanceName )
 
{
 
if ( this == window )
 
return new FCKeditor( instanceName );
 
 
...
 
}
 
</pre>
 
 
So we can use the following calls with the same results:
 
 
<pre>
 
var editor = new FCKeditor( 'editor' );
 
var editor = FCKeditor( 'editor' );
 
 
</pre>
 
</pre>

Latest revision as of 15:44, 13 September 2010

This page lists some interesting coding patterns that we may or may not use in our code.

Self Executing Functions

This is already a classic. Using a self calling function declaration to isolate its contents from the current scope, avoiding polluting it with variables.

(function()
{ 
	... 
})();

Objects with Private Stuff

var myObj = function()
{ 
	// Private variables.
	var privateVar = 10;

	var privateFunction = function()
	{ 
        ... 
	};

	// Public stuff.
	return { 
      	publicMethod : function()
		{ 
			// May access privateVar or privateFunction.
        		... 
		}
	};
}();

For Loops and Length

The "length" property should not be checked on each "for" cycle. The following construction should be used instead:

 
for ( var i = 0, len = list.length ; i < len ; i++ } 
{ 
        ... 
} 
This page was last edited on 13 September 2010, at 15:44.