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
13 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
runescape money runescape gold runescape gold runescape money buy runescape gold buy runescape money runescape money runescape gold wow power leveling wow powerleveling Warcraft Power Leveling Warcraft PowerLeveling buy runescape gold buy runescape money runescape items runescape accounts runescape gp dofus kamas buy dofus kamas Guild Wars Gold buy Guild Wars Gold lotro gold buy lotro gold lotro gold buy lotro gold lotro gold buy lotro gold runescape money runescape power leveling runescape money runescape gold dofus kamas cheap runescape money cheap runescape gold Hellgate Palladium Hellgate London Palladium Hellgate money Tabula Rasa gold tabula rasa money Tabula Rasa Credit Tabula Rasa Credits Hellgate gold Hellgate London gold wow power leveling wow powerleveling Warcraft PowerLeveling Warcraft Power Leveling World of Warcraft PowerLeveling World of Warcraft Power Leveling runescape power leveling runescape powerleveling eve isk eve online isk eve isk eve online isk tibia gold Fiesta Silver Fiesta Gold SilkRoad Gold buy SilkRoad Gold runescape accounts buy runescape accounts rs2 accounts buy rs2 accounts runescape power leveling rs2 power leveling FFXI Gil buy FFXI Gil gaia gold buy gaia gold buy dofus kamas buy dofus kamas dofus kamas runescape accounts buy runescape accounts rs2 accounts buy rs2 accounts runescape power leveling rs2 power leveling rs2 gold buy rs2 gold runescape
By
Anonymous, at 6:18 PM
???? ???? ?????? ?????? ??? ???? ???????? ???? ????? ??? ???? ??????
??? ???? ??? ??? ?????? ???? ??? ?????? ??? ???? ??? ???? ?? ??? ???? ??? RAID???? ?????? ??????? ????? ???? ???? ?? ??? Google?? ???? ???? ??? ????
Cheap WoW Gold, World Of Warcraft Gold,the best WoW PowerLeveling and other MMORPG Power LevelingWOW Gold WOWGold World Of Warcraft Gold WOW Power Leveling WOW PowerLeveling World Of Warcraft Power Leveling World Of Warcraft PowerLeveling
Our company's products are Gas Alarm,Gas Detector,Breathalyser,Breathalyzer,Alcohol Tester,Co Alarm.Breathalyzer Gas Alarm Breathalyser Co Alarm Co Detector Alcohol Tester Alcohol Tester Gas Detector Please select one of our product categories to find out more.
nh
By
Anonymous, at 2:29 AM
In wow gold players buy wow gold create cheap wow gold a character world of warcraft gold and fast wow gold adventure age of conan gold through aoc gold the ffxi gil, warhammer gold missions, runescape gold blowing tibia gold things swg credits up lotro gold and 2moons dil,maple story mesos bullets, to eve isk name lineage 2 adena but a eq2 plat few wow power leveling things. Generally wow power leveling, if you’ve power leveling seen world of warcraft power leveling it in wow leveling a matrix power leveling film wow gold chances buy wow gold are cheap wow gold you can world of warcraft gold do it wow power leveling in the power leveling game. This wow gold is cheap wow gold excellent news buy wow gold for the vast power leveling mob wow powerleveling of people wow power leveling who cheap power leveling have wow gold always buy wow gold wanted to cheap wow gold experience world of warcraft gold the power leveling matrix wow powerleveling and do wow power leveling their cheap power leveling part power leveling in helping wow powerleveling the wow power leveling people cheap power leveling of Zion. Or wow gold even buy wow gold for those cheap wow gold people world of warcraft gold who wow gold were secretly wow geld sympathetic wow gold kaufen to the billig wow gold Machines, or those wow gold who found cheap wow gold the Merovingian buy wow gold charming bolts nuts and were nut and bolt secretly rooting for his Exiles throughout the film.
wow gold
buy wow gold
cheap wow gold
By
Anonymous, at 7:44 PM
Raju and Savitri lived in wow goldTanjore in the state of Tamil Nadu. Raju worked in the rice fields ofwow gold a local landlord or zamindar for six months a year. In turn, he received wow goldfour sacks of rice as salary. He would then sell three of them in the local marketwow gold and keep one sack for himself for food. At the local market he would exchange the rice for wow goldvegetables, flour, and groceries. Savitri would do the cooking and washing and look after the home. Onewow gold year there was a severe drought. There was no rain and the rice fields dried up. By April, the two were wow goldgetting tired of eating nothing but rice. So one day, they decided to go digwow gold for tubers. They took an axe to cut firewood and set off. Throughout thewow gold day Raju cut branches and Savitri searched around for tubers and mushrooms. In the wow goldevening they sat under a tree exhausted. Raju put down his axe nearby and leaned his back tiredly against a tree.
By
Anonymous, at 4:18 PM
Our story today is called "Pigs Is Pigs". It was written by Ellis Parker Butler. Here isworld of warcraft gold Shep O'Neal with the story.
Mike Flannery, the agent wow powerlevelingof the Interurban Express Company leaned over runescape moneythe desk in the company's office in Westcote and shookugg boots his fist. Mister Morehouse, angry world of warcraft goldand red, stood on the runescape goldother side of the desk, shaking with fury. The argument had been long wow power levelingand hot. At last, Mister Morehouse ugg bootshad become speechless. The cause of the trouble lay on the desk between the two men. It was a box with two Guinea pigs inside."Do asyou like them." shouted Flannery, "Pay for them and take them. Or don't pay forugg boots them and leave them here. Rules aredofus kamasrules, Mister Morehouse. And Mike Flannery is not going to break them." "But you stupid idiot!" shouted Mister Morehouse, madlyworld of warcraft gold shaking a thin book beneath the dofus kamasagent's nose. "Can't you read it here in your own book of transportation rates? Pets, domestic, Franklin toLord of the Rings Online Gold Westcote, if LOTRO Goldcorrectly boxed, twenty-five fly for fun penyacents each." He threw flyff penyathe book on the desk. "What more do you want? Aren't they Pets, Aren't they domestic? Aren't they correctly boxed? What?"He turned and walked Final Fantasy XI gilback and forth rapidly with a ffxi gilfurious look on his face. "Pets, " he said,"P-E-T-S, twenty-five cents each, twoeq2 plat times twenty-five is fifty. Can you understand eq2 goldthat? I offer you fifty cents." Flannery reached Lord of the Rings Online Goldfor the book. He ran wow powerlevelinghis hand through the pages LOTRO Goldand stopped at page sixty-four. "I don't take flyff penyafifty cents." he whispered inbuy flyff gold a non-pleasant voice. "Here's theffxi gil rule for it.' WhenLord of the Rings Online gold the agent be anybuy ffxi gil doubt about which two rates eq2 platshould be charged on a shipment, he shall eq2 goldcharge the LOTRO goldlarger. The person receiving the shipment fly for fun penyamay put in a claim for the flyff penyaovercharge. ' In this case, Mister Morehouse, I be Final Fantasy XI gilin doubt. Pets ffxi gilthem animals may be. And domestic they eq2 platmay be. But pigs, I am sure they do be. And my rule says plain as the nose eq2 goldon your face, pigs, Franklin to Westcote, thirty cents each."
By
Anonymous, at 4:18 PM
Mister Morehouse shook his head savagely. "Nonsense," he shouted,"confounded nonsense! I tell you. That rule means common pigs, not Guinea pigs." "Pigs is pigs." Flannery said firmly. Mister Morehouse bit his lip and thenworld of warcraft gold flung his arms out wildly. "Very well," he shouted " You shall hear of this. Your president shall hear of this. It is an outrage. I have offered you fifty cents. You refuse world of warcraft goldit. Keep the pigs until you are ready to take the fifty cents. But, by Gorge, Sir, if one hair of those pigs' heads is harmed, I will have the law on you." He turned and walked wow power levelingout, slamming the door.
Flannery carefully lifted the box from the desk and put it in a corner. Mister Morehouse world of warcraft goldquickly wrote a letter to the president of the transportation express company. The president answered, informing Mister Morehouse that all claims for overcharge should be sent to the Claims Department. Mister Morehouse wrote to the Claims Department. Onecheap wow gold week later, he received the answer. The Claims Department said it had discussed the matter with the agent at Westcote. The agent said Mister world of warcraft goldMorehouse had refused to accept the two Guinea pigs shipped to him, therefore the department said Mister Morehouse cheap wow goldhad no claim against the company and should write to its Tariff Department. Mister Morehouse wrote toworld of warcraft gold the Tariff Department. He stated his case clearly. The head of the Tariff Department read Mister Morehouse's buy wow goldletter. "Ah,Guinea pigs," he said,"probably starved to death by this time." He wrote to the agent, asking why the world of warcraft goldshipment was held up. He also wanted to know if the Guinea pigs were still in good health.Before answering, agent wow power levelingFlannery wanted to make sure his report was up-to-date, so he went to the back of the office and looked intoworld of warcraft gold the cage. Good lord, they were now eight wow gold kaufenof them. All well,and eating like hippopotamuses. He went back to the dofus kamasoffice and explained to the head of the Tariff Department what the rules said about pigs. And as for the condition of thekamas dofus Guinea pigs, said Flannery, they were all well, but there were eight of them now,all good eaters.
By
Anonymous, at 4:19 PM
The point man hadwow goldthe puck during Minnesota's fourth straight power play, which came at 6:20 of the second period. There wasn't a shootingwow goldlane. The Wild didn't have much traffic in front.Butwow goldthe power-play specialist did whatwow goldhe's been coached to do: create an opportunity where onewow golddoesn't exist. So by fading off the right point and walking the puck towow goldthe middle of the ice.
By
chengja, at 4:43 PM
Zidlicky's laser atdofus kamaskamas dofusacheter kamasbuy kamas7:29 was the only goal in theacheter dofusdofus kamaskamas dofusacheter dofusbuy kamasBruins' 1-0 loss to the Wild before 16,272 at TD Banknorthacheter kamasdofus kamaskamas dofusacheter kamasdofus kamaskamas dofusachat kamasdofus kamaskamas dofusGarden last night. The Bruins, who fellacheter des kamasdofus kamaskamas dofusbuy kamaswow pobuy wow goldachat gold wowwow levelingto the Sabres Saturday by a 4-2 score, havewow orwow gold cheapwow powerlevelingbuy wow goldworld of warcraft goldcheap wow goldwow orwow power levelinglost back-to-back matches for the first time this season, although given the way they competed last night and put pressure on Minnesota goalie Niklas Backstrom, they weren't about to call this a crisis."You don't panic," said coach Claude Julien. "I don't think anybodyworld of warcraft goldwow gold kaufenwow gold cheapwow levelwow geldwow gold kaufenwow gold cheapwow powerlevelingthought we were going to be flying the way we were flying for 82 games without going through some bumps and bruises.
By
chengja, at 4:43 PM
But the Bruinsworld of warcraft goldwow powow orbuy wow goldcheap wow goldtipped their hats to the Wild, whowow power levelingwow powerlevelingdofus kamaskamas dofusLord of the Rings Online GoldLOTRO GoldLOTR Goldfly for fun penyaplayed their clog-'em-up, defense-firstflyff penyabuy flyff goldffxi gilbuy ffxi gilFinal Fantasy XI gilEverQuest 2 goldeq2 platgame to perfection afterffxi gilFinal fantasy xi gilbuy ffxi gildofus kamaskamas dofustaking the lead. They took away therunescape moneyrunescape golddofus kamaskamas dofusRunescape MoneyRunescape goldfly for funcenter of the rink.
By
chengja, at 4:44 PM
Tbcgold est un site webdofus kamaschanger la monnaie virtuelle pour que vous, notre client, gagniezwow golddes meilleures expériences dans ce jeu !
By
chengja, at 4:44 PM
Post a Comment
<< Home