12 ms·
Javascript the right way
- tnorthcutt 14y agoWhy does this site say (at the bottom, under the license) Based on a work at http://jstherightway.com http://jstherightway.com. ? I noticed that PHP The Right Way (the inspiration for this site) says something similar: Based on a work at www.phptherightway.com.
- projectedoptics 14y agoSince it's on Github - I guess it's for anyone who might fork and modify it, so that there's a link back to the original: https://github.com/braziljs/js-the-right-way https://github.com/braziljs/js-the-right-way
- tnorthcutt 14y agoAh, that makes sense (as does the answer given by sjs382). Thanks.
- sjs382 14y agoIs it because it's CC BY-NC-SA? And showing you how they prefer to be given attribution?
- tnorthcutt 14y agoI really like the nav highlighting used on this (and http://phptherightway.com http://phptherightway.com). Specifically, I like that it highlights the nav element(s) for whichever sections are in view, rather than highlighting only one nav element at a time. Very elegant.
- jlogsdon 14y agoCheck out Bootstrap's Scrollspy[1]. Its one implementation (of probably many) that I've seen of the nav highlighting that you mentioned. [1] http://twitter.github.com/bootstrap/javascript.html#scrollspy http://twitter.github.com/bootstrap/javascript.html#scrollsp...
- ruxkor 14y agoA very nice initial listing for applications, I'd love to see it completed! One framework I think is missing in the category "server-side" is derby.js ( http://derbyjs.com http://derbyjs.com ), have you thought about adding it to the list?
- lukifer 14y agoI loves me some Derby (and Racer), but it's not ready for production use until it supports server-side authentication logic. (Supposedly it's on the way.)
- TazeTSchnitzel 14y agoNo mention of Crockford? :(
- debacle 14y agoWith all due respect, Crockford jumped the shark some time ago.
- TazeTSchnitzel 14y agoIn what sense?
- karlshea 14y agoProbably in the sense that half the community got butthurt over his rant about semicolons.
- debacle 14y agoCrockford is as important to modern JS as Lerdorf is to modern PHP. He has a very minimal role in the community, and most of that involves resting on his laurels and being divisive.
- TazeTSchnitzel 14y agoReally? I'd see him as quite important to modern JS. Heck, he discovered JSON, that's certainly important, and he helps shape the ECMAScript standards. To ignore him would be foolish.
- ojr 14y agoDouglas Crockford should be named in my opinion, he loves javascript! ... Javascript: The Good Parts is a pretty good book, good real world examples, like one where Crockford shows you how to split a url with regular expressions in javascript JSlint is a pretty good tool, great parsing, using C, going on in the source JSON is great for data. Data is pretty important.
- 14y ago
- jarek-foksa 14y agoThe way how Addy Osmani implements singleton pattern is rather disputable. I would implement singleton as a regular object that returns itself immediately if initialized for the second time. His prototype pattern also doesn't look like the right way of doing things. Is there actually anyone that uses plain object descriptors? I would just create helper method that wraps Object.create() and takes prototype object as first argument and instance object as second argument. There is no need to use constructors in this pattern (either directly or inside helpers) unless you are targetting legacy browsers. I also dislike the fact that public/local scope is emulated by misusing the return statement - this is ugly, confusing and it doesn't play nicely with other patterns such as prototype. If you need some notion of public/private scope then just use underscore notation.
- Androsynth 14y agoThe singleton pattern he presents is pretty common and pretty powerful. It is a self-invoking module. It allows you to use the module pattern (very solid js pattern) and probably has less code than your method. Also, javascript has closures, use them. this._private is a hacky java-esque way of doing things. return {public_methods...} is a solid way of working towards the strengths of the language and doing proper js encapsulation. As for the prototype pattern, there are a million ways you can do that. Best to learn the fundamentals so you can use whatever works best based on the situation. edit: I should add that one of the reasons I like JS patterns is that there are multiple correct ways to implement them based on the situation. There are many wrong ways too, of course :) but it is a very flexible language.
- jarek-foksa 14y agoHere is what I prefer to call a singleton in JS, I don't think it could be any simpler than that: mySingleton = { initialized: false, init: function() { if (this.initialized == false) { this.initialized = true; // Initialize the singleton here } return this }, } singleton1 = mySingleton.init() singleton2 = mySingleton.init() Both underscore notation and returning "public" methods feel awkward in JavaScript, though the second appraoch introduces major trade-offs which I belive are not worth it. E.g. how do you create inheritance chains when using this pattern? Are you just copying public methods returned from one object into another? Or how do you inspect private properties in Dev Tools? Are you setting breakpoints for that purpose?
- Ramone 14y agoLooks more like JS the java way. Singletons make no sense in a language that has global variables and never lets you stop an object from being duplicated. The command pattern makes no sense in a language with first-class functions. I'd steer clear of this resource if you want to write javascript the right way.
- tptacek 14y agoObligatory: http://norvig.com/design-patterns/ http://norvig.com/design-patterns/
- nahname 14y agoAlmost every javascript library you use is a singleton, so your second sentence makes no sense. Check the example, it is just the module pattern. If a programmer wants to deviate away from a pattern, the language lock-in won't stop them. Also, the command pattern is a great tool for handling undo/redo on a UI. I think you are being way to dismissive of some good advice. Just because someone is trying to share a better way of doing things that is based on ideas created in another language does not detract from their value.
- debacle 14y agoCan you give an example of a JavaScript library that is a singleton?
- se85 14y agoI somewhat agree with your points. You can't just explain every single design pattern available in any given language and then say, you now know how to write that language "The right way!". Thats the feeling I got from this, it just goes into way too much detail about way too many things, and forgets to start with the basics, which is all you need to know to write JavaScript the right way. How can you write any language the right way without knowing the basics? This book could be an absolute disaster in the hands of people who don't know any better. +1 for steering clear of this resource.
- 14y ago
- gnuwilliam 14y agoHi guys! This a WIP, and we'd like to ask for your help to get this guide more complete! It's a live document. Please, feel free to fork the project and send a pull request, we really want to make this guide a reference for JavaScript developers.
- eterps 14y agoIf you use JavaScript on the client side, you do it to drive the user interface which consists solely of DOM element objects. So why not make those DOM elements object oriented instead: https://github.com/hafriedlander/jquery.entwine https://github.com/hafriedlander/jquery.entwine
- jarek-foksa 14y agohttp://i.imgur.com/Ml2Jb.jpg http://i.imgur.com/Ml2Jb.jpg Sorry, I couldn't resist...