1. Better code

Created Saturday 14 June 2014

window.onload = function () {
alert("The window is loaded!");
}
// sort by string length
myStrs = new Array("one, "two", "three", "four", "five", "six");
myStrs.sort(function(a, b) {
return (a.length - b.length);
});
// If returns < 0 than 'a' comes before 'b', else 'b' comes before 'a'
for (i = 0; i < finalTest; i++) {
if (!someCondition)
continue;
......
}
fTotal = 0.0;
for (stock in portfolio) {
fTotal += portfolio[stock];
}
var cElems = myArray.length;
for (var i=0; i < cElems; i++) {
.....
myArray.pop(); // Removes the last element and updates the length of an array
}
function f(x) {
x += 5; // no effect
}
function h() {
var x = 5;
f(x); // x is still 5
}
function f(obj) {
obj.d = new Date(2009, 12); // will have an effect
}
function h() {
var x = new Object();
x.d = new Date();
f(x); // x.d has changed
}
for (var j = 0; j < document.getElementsByTagName('a').length; j++) {
var sPrefixStr = "Found element: ";
var aElements = document.getElementsByTagName('a');
var sElemStr = sPrefixStr + aElements[i].tagName;
alert(sElemStr);
}
var aElements = document.getElementsByTagName('a');
var sPrefixStr = "Found element: ";
var cElems = aElements.length;
for (var j = 0; j < cElems; j++) {
alert(sPrefixStr + aElements[i].tagName);
}
with (test.information.settings.files) {
primary = 'names';
secondary = 'roles';
tertiary = 'references';
} // We don't have to write the full path to a property, like test.information.settings.files.primary = 'names' ...
var temp = test.information.settings.files;
temp.primary = 'names';
temp.secondary = 'roles';
temp.tertiary = 'references';
function addMethodToObj(oObject, oProperty, sFunctionCode) {
oObject[oProperty] = new Function(oFunctionCode);
}
addMethodToObj(oMyObj, 'rotate90', 'this.anlge=(this.angle+90)%360');
addMethodToObj(oMyObj, 'rotate60', 'this'angle=(this.angle+60)%360');
function addMethodToObj(oObject, oProperty, fFunctionCode) {
oObject[oProperty] = fFunctionCode;
}
addMethodToObj(oMyObj, 'rotate90',
function () {this.angle = (this.angle + 90) % 360});
addMthodToObj(oMyObj, 'rotate60',
function () {this.angle = (this.angle + 60) % 360});
setInterval("displayData()", 1000);
setTimeout("moveElementBy(x); x+=5;", 1000);
function displayData() { ... };
setInterval(displayData, 1000);
setTimeout(function () {
moveElementBy(x);
x+=5;
}, 1000);
var sNewStr = "";
sNewStr = "str x" + "str y" + "str z"; // causes temp string creation
var sNewStr = "";
sNewStr += "str x";
sNewStr += "str y";
sNewStr += "str z";
element.innerHTML = "<div><p>Some text</p></div>";
var oOrigElem = document.getElementById('myDiv');
var oClone = oOrigElem.cloneNode(true); // 'true' for cloning the contents too
oClone.setAttribute('style', 'width:50%');
var oNewElem = null, sContent = "";
var cStrs = asStrings.length; // suppose this is a big array
for (var i = 0; i < cStrs; i++) {
oNewElem = document.createElement('p');
sContent = document.createTextNode(asStrings[i]);
oNewElem.appendChild(sContent);
oClone.appendChild(oNewElem);
}
oOrigElem.parentNode.replaceChild(oClone, oOrigElem);
if (!com_joemarini) com_joemarini = {};
else if (typeof (com_joemarini) != "object")
throw new Error ("com_joemarini not an object!");
com_joemarini.sNAME = "Name of my module";
com_joemarini.sVERSION = "1.0";
com_joemarini.method1 = function () {
.....
}
com_joemarini.aProperty1 = [];
com_joemarini._sPrivateProp = "";



Backlinks: