Find the secrets to infinite income, and automate it!
23 Feb
The examples here are taken from Function page of the Prototype JS api documentation. They have been re-written to reflect the Prototype XUL changes. Hope they help!
var fn = function(foo, bar) { return foo + bar; }; argumentNames(fn); //-> ['foo', 'bar']
var fn = function(foo, bar) { return foo + bar; }; argumentNames(fn); //-> ['foo', 'bar']
var obj = { name: 'A nice demo' }; function handler(e) { var tag = Event.element(e).tagName.toLowerCase(); var data = $A(arguments); data.shift(); alert(this.name + '\nClick on a ' + tag + '\nOther args: ' + data.join(', ')); } Event.observe(document, 'click', bindAsEventListener(obj, handler, 1, 2, 3));
String.prototype.splitOnSpaces = curry(String.prototype.split, " "); "foo bar baz thud".splitOnSpaces(); //-> ["foo", "bar", "baz", "thud"]
function hideNewElement() { $('inserted').hide(); }; function insertThenHide(markup) { $('container').insert(markup); // Prototype XUL defer(hideNewElement); } insertThenHide(" <p id="inserted">Lorem ipsum ");
// before: window.setTimeout(function() { Element.addClassName('foo', 'bar'); }, 1000); // after: delay(Element.addClassName, 1, 'foo', 'bar'); // clearing a timeout var id = delay(Element.hide, 5, 'foo'); window.clearTimeout(id);
// start off with a simple function that does an operation // on the target object: var fn = function(target, foo) { target.value = foo; }; var object = {}; // use the original function fn(object, 'bar'); object.value //-> 'bar' // if we methodize it and copy over to the object, it becomes // a method of the object and takes 1 argument less: object.fnMethodized = methodize(fn); object.fnMethodized('boom!'); object.value //-> 'boom!'
String.prototype.capitalize = wrap(String.prototype.capitalize, function(proceed, eachWord) { if (eachWord && this.include(" ")) { // capitalize each word in the string return this.split(" ").invoke("capitalize").join(" "); } else { // proceed using the original function return proceed(); } }); "hello world".capitalize() // "Hello world" "hello world".capitalize(true) // "Hello World"
Leave a reply