Coding Style: Please Don't Build JavaScript Classes Like This!
I just picked up DHTML Utopia: Modern Web Design Using JavaScript & DOM by Stuart Langridge. Overall this is an excellent, informative book, and one of the only books out there with up-to-date AJAX information. However, I was extremely disappointed at their choice in OOP style.
The author suggests building "Library Objects" like this one:
Here are some of the reasons why I think this is poor practice:
If you're going to use OOP in JavaScript (and you should!) you really need to use the techniques in the seminal JavaScript: The Definitive Guide from O'Reilly. Please use constructors and
The author suggests building "Library Objects" like this one:
var myObj = {
prop: "hello",
method1: function() {
document.write("this is method1");
},
method2: function() {
document.write(myObj.prop + "");
}
}//end myObj
myObj.method1(); //output: this is method1
myObj.method2(); //output: hello
Here are some of the reasons why I think this is poor practice:
- You can't instantiate multiple copies of your object.
- You don't have a constructor function that can be called when the object is created.
- The syntax requires an additional level of nesting.
- It's easy to forget to add a comma between items and create an error.
If you're going to use OOP in JavaScript (and you should!) you really need to use the techniques in the seminal JavaScript: The Definitive Guide from O'Reilly. Please use constructors and
prototype and new. As described in a previous post, the ideal way to create a Class for the Object listed above is:
var myObj = function(){
this.prop = "hello";
}
myObj.prototype.method1 = function(){
document.write("this is method1");
}
myObj.prototype.method2 = function(){
document.write(this.prop);
}
var obj = new myObj();
obj.method1(); //output: this is method1
obj.method2(); //output: hello
3 Comments:
I'm going to disappoint you here: I don't agree with you. (Unsurprising, eh?) I don't think of the objects I'm creating as reusable "library objects". It's simply a way of packaging up the code for that page in a way that ensures that it won't conflict with any other libraries on the page. They're not supposed to be a "reusable object"; creating multiple copies of the object thus created would be entirely pointless. If I wanted a real proper object for reuse then it would be a separate object with a constructor and created using prototypes and whatnot. However, to my mind, that isn't really very useful in most cases; it's Java-ish object-orientation gone mad. The idea that everything should be a reusable object makes hacking a pain, I think. Therefore, I don't agree with the first three of your comments about why it's a bad idea. You're right about forgetting to put commas in, though, which is a weakness. However, doing all the prototype stuff is even harder, especially for people who aren't super-l33t JS hackers like you and me. :)
By
Stuart Langridge, at 2:52 AM
Hi Stuart,
Thanks for posting, and I hope I wasn't too hard on you. I did say it was an excellent book! :)
Your point about these being Library Objects rather than Reusable Objects is well taken, and in this case instantiating them with the { , , } syntax is probably ok.
I guess my main problem was that I felt like dumped this notion of "Library Objects" on the reader without providing much context. If it confused me, I imagine it might have confused other people as well.
I'm going to take issue with your statement that it isn't useful in JavaScript. I honestly feel it does have a place, though not necessarily in exactly the same way as Java OOP. You might check my Article on the topic...
Time permitting, I will to go back and re-read some parts of your book, and then I'll try to respond with further comments.
By
adampasz, at 5:20 PM
wow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro goldwow power leveling
wow powerleveling
world of warcraft power leveling
ffxi power leveling
ffxi powerleveling
ffxi
ffxi gil
age of conan
lotro powerleveling
lotro power leveling
lotro gold
By
wow power leveling, at 12:47 AM
Post a Comment
<< Home