Recently read a JS book called "JavaScript Advanced Program Design" in the inside learned a lot of things, is a good book, it is worth a look.
Decoupling Css/javascript
Element.style.color= "Red";
Element.style.backgroundcolor= "Blue";
The way CSS and JavaScript are too close, we should write:
Element.classname= "edit";
CSS style and JS code completely separate.
Optimize again:
var cssname={
Css1: "Edit",
}
Element.classname=cssname.css1;
2. Decoupling application logic/event handlers
function Handlekeypress (event) {
Event=eventutil.getevent (event);
if (event.keycode==13) {
var target=eventutil.gettarget (event);
var value=5*parentint (Target.value);
if (value>10) {
Document.getelement ("Error-msg"). style.display= "Block";
}
}
};
Written:
function Validatevalue (value) {
Value=5*parseint (value);
if (value>10) {
Document.getelement ("Error-msg"). style.display= "Block";
}
};
function Handlekeypress (event) {
Event=eventutil.getevent (event);
if (event.keycode==13) {
var target=eventutil.gettarget (event);
}
};
3. Avoid global Volume:
var name= "Ncihoals"
function Sayname () {
}
Written:
var myapplication={
Name: "Nicholas",
Sayname:function () {
}
}
The concept of the JS namespace:
Creating global objects
var wrox={};
To create a namespace:
wrox.projs={};
Append all the variables used to the Wrox
wrox.projs.eventutil={};
The main purpose of this is to be able to coexist with other JS files on the same page without duplicate names.
3. Avoid comparisons with null:
function Sortarry (values) {
if (values!=null) {//Avoid
Values.sort (Comparator);
}
}
function Sortarry (values) {
if (values instanceof Arry) {//Recommended
Values.sort (Comparator);
}
}
JS maintainability Code