function showStatistics(args) {
document.write("<p><strong>Name:</strong> " + args.name + "<br />");
document.write("<strong>Team:</strong> " + args.team + "<br />");
if (typeof args.position === "string") {
document.write("<strong>Position:</strong> " + args.position + "<br />");
}
if (typeof args.average === "number") {
document.write("<strong>Average:</strong> " + args.average + "<br />");
}
if (typeof args.homeruns === "number") {
document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />");
}
if (typeof args.rbi === "number") {
document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>");
}
}
showStatistics({
name: "Mark Teixeira"
});
showStatistics({
name: "Mark Teixeira",
team: "New York Yankees"
});
showStatistics({
name: "Mark Teixeira",
team: "New York Yankees",
position: "1st Base",
average: .284,
homeruns: 32,
rbi: 101
});
6 Advanced JavaScript Techniques You Should Know - 3 views
-
-
Object-oriented JavaScript implements namespace-like principles due to the fact that properties and methods are declared inside of objects, thus there are less likely to be conflicts. A conflict could arise, however, through object names. And very likely, the conflict will occur "silently", thus you may not be alerted to the issue immediately.
-
if (typeof MY == "undefined") { MY = new Object(); MY.CUSTOM = new Object(); } MY.CUSTOM.namespace = function() { function showStatistics(args) { document.write("<p><strong>Name:</strong> " + args.name + "<br />"); document.write("<strong>Team:</strong> " + args.team + "<br />"); if (typeof args.position === "string") { document.write("<strong>Position:</strong> " + args.position + "<br />"); } if (typeof args.average === "number") { document.write("<strong>Average:</strong> " + args.average + "<br />"); } if (typeof args.homeruns === "number") { document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />"); } if (typeof args.rbi === "number") { document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>"); } } showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); } MY.CUSTOM.namespace();
- ...1 more annotation...
-
//2. Object Literals to Pass Optional Arguments function showStatistics(args) { document.write("Name: " + args.name + "
"); document.write("Team: " + args.team + "
"); if (typeof args.position === "string") { document.write("Position: " + args.position + "
"); } if (typeof args.average === "number") { document.write("Average: " + args.average + "
"); } if (typeof args.homeruns === "number") { document.write("Home Runs: " + args.homeruns + "
"); } if (typeof args.rbi === "number") { document.write("Runs Batted In: " + args.rbi + ""); } } showStatistics({ name: "Mark Teixeira" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); //Using Namespaces to Prevent Conflicts if (typeof MY == "undefined") { MY = new Object(); MY.CUSTOM = new Object(); } MY.CUSTOM.namespace = function() { function showStatistics(args) { .................. } showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 }); } MY.CUSTOM.namespace();
12 Tips to improve your jQuery code | Geekology - 2 views
-
DOM manipulation is one of the slowest operations JavaScript (and hence, jQuery) can perform.
-
Use IDs instead of classes in selectors
-
When you need to perform multiple methods on elements, jQuery can chain those methods together.
- ...2 more annotations...
Google Closure: How not to write JavaScript - 3 views
-
“It’s a JavaScript library written by Java developers who clearly don’t get JavaScript.”
-
Unfortunately, unlike the built-in properties supplied by Object.prototype, custom properties added to Object.prototype will show up as an object property in any for-in loop in the page.
-
custom properties added to
- ...3 more annotations...
1 - 4 of 4
Showing 20▼ items per page