What we want is the ability to define a method dynamically (given a name) that is also a closure over the lexical scope at the point of method definition.
color_name = 'black' Ninja.send(:define_method, 'color') do puts "#{name}'s color is #{color_name}"end
var colorName = "black"; Ninja.prototype['color'] = function () { puts(this.name + "'s color is " + colorName);}
The ubiquity of closures in Javascript is extremely powerful and, as we have seen so far, makes metaprogramming very easy.
You can think of a metaclass as a class definition specific to a single instance of a class.
This means we can add methods to an object’s metaclass without adding the same behavior to all instances of that object’s class.
This means that Javascript does not distinguish between classes/prototypes and instances and, therefore, we can add our desired behavior directly to the instance.
Underscore is a utility-belt library for Javascript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in Javascript objects. It's the tie to go along with jQuery's tux.
Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects. It's the tie to go along with jQuery's tux.
Collections
each, map, reduce, detect, select, reject, all, any, include, invoke, pluck, max, min, sortBy, sortedIndex, toArray, size
Arrays
first, last, compact, flatten, without, uniq, intersect, zip, indexOf, lastIndexOf
Functions
bind, bindAll, delay, defer, wrap, compose
Objects
keys, values, extend, clone, isEqual, isElement, isArray, isFunction, isUndefined
Utility
noConflict, identity, uniqueId, template
Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects. It's the tie to go along with jQuery's tux, and Backbone.js's suspenders.
As the first online Regular Expression tester to offer realtime highlighting and offering more languages than any other online tester, ReJAx continues to be the coolest and easiest to use Regular Expression tester on the web. Supported languages: * JavaScript * PHP 5 PCRE * PHP 5 POSIX * Ruby - BETA!! * PERL 5 * UNIX Shell Pattern It is not necessary to enter the beginning and ending slashes in the pattern field. Not all modifiers will be compatible with every language. Backreferences (when available) are usually in the form of \n where n is the reference number