Here’s a trick I learned from Valerio (author of Mootools) on how to handle options for classes and functions. After refactoring a couple of legacy scripts I thought I’d share for those of you who might not know it already. This example below uses the Mootools syntax for class creation, but the rest of it can be used even on its own I think. Read the rest of this entry »
Archive for the ‘Reference’ Category
this.options – setting defaults that can be overwritten in your classes
November 14th, 2006 by Aaron N.Optimizing Page Load Time
November 1st, 2006 by Aaron N.via ajaxian (as usual):
Aaron Hopkins of Google has released an article on Optimizing Page Load Time which came out of his experience optimizing page load times for a high-profile Ajax application.
He starts off talking about “how much I could reduce latency due to external objects. Specifically, I looked into how the HTTP client implementation in common browsers and characteristics of common Internet connections affect page load time for pages with many small objects.” Read the rest of this entry »
javascript reference javadoc style
October 10th, 2006 by Aaron N.In relation to my previous post on the topic, I was digging around for javadoc options and stumbled upon this javascript reference in javadoc form. It’s quick and easy to use like any javadoc and it’s going in my bookmarks right now.
CNET Global Framework update
October 3rd, 2006 by Aaron N.Hi all,
It’s been a few days since I posted. I’ve been heads down on the project to create a global javascript framework for our sites (Redball, at least). I’ve finished this work (or at least gotten it to a releasable state) and I thought I’d share an update. I plan on documenting it extensively and providing a lot of examples (and teaching it) in the near future, but for those of you who are curious, you can peruse the library now. It’s mostly stable (we’re still tweaking and adding things) and you can just download them and read them if you want to see what’s in them.
They include a lot of functionality, are highly extendable, easily debuggable, and hopefully will be useful. The current framework file is about 30K, but Andy just turned me on to a more efficient javascript compressor that will bring that down to 19K. I can’t express how awesome it is to have this thing going out and it managing to pack in so much functionality in such a small package. Read the rest of this entry »
First look: CNET (Redball) Global Javascript Libraries
September 25th, 2006 by Aaron N.I’ve been working on a global library for cnet javascript standards and I’ve got my first crack at it ready. I’m going to spend some time in the coming weeks working on documentation and examples, but for those of you interested, here’s a quick look at what’s in the collection. Read the rest of this entry »
Some early blog posts on MooTools w/ examples
September 20th, 2006 by Aaron N.MooTools continues to impress me. Check out the Class.implement and Class.extend functionality in this great article from Cory Hudson (there are numerous code examples).
mootools implements a class inheritance scheme that is inspired by Dean Edwards’ wonderful Base class. Creating a class is similar to Prototype, but now you don’t ever have to think about the prototype object when you define the class…
…mootools supports multiple inheritance in a Ruby mixin style with the
Class.implement()method. Using the implement method of an existing class, that class can inherit methods and properties from another class or object. UnlikeClass.extend(), theimplementmethod is called on the class you are modifying, not on the object being copied. This method can be used to support multiple inheritance.
And here’s another article from Cory on MooTools numerous helpful utility functions:
The new mootools JavaScript framework has quickly impressed me with its design and usefulness. The library was clearly written to meet real programmers’ needs while working in JavaScript. Just take a look at some of the new utility functions and methods it provides.
Note: This article covers functions and methods found in the Array.js and Function.js modules of mootools.
Cutting down on loop iterations with labels
September 20th, 2006 by Aaron N.Here’s a short little description of how to use labels, breaks, and continues to cut down on loop iterations and speed up your code.
Just a quick reminder that you can drastically cut down on loop iterations by using the break and continue commands, and that there is an option to label loops to allow nested loops to stop their parents from iterating.
Mootools
September 19th, 2006 by Aaron N.Well crap. 1) I love the mad4milk guys (makers of moo.fx, moo.ajax, moo.dom, prototype.lite). 2) their new framework looks AWESOME. 3) as always, their libraries are SUPER TINY.
But damn, now I have to learn something new, and maybe rewrite a bunch of crap. This is the problem with javascript. Still, when Prototype + Scriptaculous is 100K, you gotta admire their ability to crank something out in under 20K that will get you nearly the same thing.
The Mad4Milk team (the minds that brought the world moo.fx) have unleashed a brand new, very impressive Javascript library out onto the web – MooTools.
mootools is a very compact, modular, Object-Oriented javascript framework. Its unique design makes it extremely crossbrowser, easy to use, and a snap to extend with your own code. It comes with a choice of more than fifteen scripts, plugins and addons, including Effects (moo.fx) Ajax (moo.ajax), Dom Navigator (moo.dom), Drag and Drop, Sortable lists, cookies Manager and many more.
There aren’t any demos of the functionality quite yet (as of the date of this post), but you can download the first release of thise powerful little tool.
You can also check out what Jonathan Snook has to say about it, having already downloaded and worked with it a bit. He’s also created a simple tutorial on using the new library to create a drag-and-drop example.
Scope in Javascript
September 19th, 2006 by Aaron N.via ajaxian (surprise):
Mike West has put some time into analyzing and understanding one of the more sticky issues in Javascript: scope.
Scope is one of the foundational aspects of the JavaScript language, and probably the one I’ve struggled with the most when building complex programs. I can’t count the number of times I’ve lost track of what the this keyword refers to after passing control around from function to function, and I’ve often found myself contorting my code in all sorts of confusing ways, trying to retain some semblance of sanity in my understanding of which variables were accessible where.
He has published his explorations as an article in Digital Web Magazine. In it he deals with the basics of scope; the
thiskeyword in method calls, constructors, function calls and event handlers; theapply()andcall()methods; and Prototype’sbind()extension to Function.The article is illustrated throughout with code examples and includes a list of useful references at the end. A good addition to the family of online Javascript resources.
JavaScript Closures for Dummies
September 19th, 2006 by Aaron N.via ajaxian – note that closures are one big cause of memory leaks…
Morris Johns has gone though and written a detailed explanation of closures by example:
If everything seems completely unclear then the best thing to do is to play with t e examples. Reading an explanation is much harder than understanding example
My explanations of closures and stack-frames etc are not technically correct – they are gross simplifications intended to help understanding. Once the basic idea is grokked, you can pick up the details later.Final points:
- Whenever you use function inside another function, a closure is used.
- Whenever you use eval() inside a function, a closure is used. The text you eval can reference local variables of the function, and within eval you can even create new local variables by using eval(‘var foo = ‘
- When you use Function() inside a function, it does not create a closure. (The new function cannot reference the local variables of the function calling Function()).
- A closure in JavaScript is like keeping a copy of the all the local variables, just as they were when a function exited.
- It is probably best to think that a closure is always created just on entry to a function, and the local variables are added to that closure.
- A new set of local variables is kept every time a function with a closure is called (Given that the function contains a function declaration inside it, and a reference to that inside function is either returned or an external reference is kept for it in some way).
- Two functions might look like they have the same source text, but have completely different behaviour because of their ‘hidden’ closure. I don’t think JavaScript code can actually find out if a function reference has a closure or not.
- If you are trying to do any dynamic source code modifications ( for example: myFunction = Function(myFunction.toString().replace(/Hello/,’Hola’)); ), it won’t work if myFunction is a closure (Of course, you would never even think of doing source code string substitution at runtime, but…).
- It is possible to get function declarations within function declarations within functions – and you can get closures at more than one level.
- I think normally a closure is the term for both the function along with the variables that are captured. Note that I do not use that definition in this article!
- I suspect that closures in JavaScript differ from those normally found in functional languages.