Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created December 21, 2009 18:07
Show Gist options
  • Select an option

  • Save subtleGradient/261116 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/261116 to your computer and use it in GitHub Desktop.
String.prototype.call && Array.prototype.apply
Array.prototype.apply = function(bind, args){
var array = new Array(this.length);
for (var i=0; i < this.length; i++) {
if ('call' in this[i] && typeof this[i].call == 'function'){
try {
if (typeof this[i] == 'function')
array[i] = this[i].apply(bind, args);
else array[i] = this[i].call.apply(bind, args);
continue;
} catch(error){
array[i] = error;
if (Array.prototype.call.debug) continue;
}
}
array[i] = null;
}
return array;
};
Array.prototype.call = function(bind, args){
args = Array.prototype.slice.call(arguments);
args.unshift();
return Array.prototype.apply.call(this, bind, args)
};
String.prototype.apply = function(bind, args){
if (typeof bind[this] == 'function')
return bind[this].apply(bind, args);
else return bind[this];
};
String.prototype.call = function(bind, args){
if (typeof bind[this] == 'function'){
args = Array.prototype.slice.call(arguments);
args.unshift();
return bind[this].apply(bind, args);
}
else return bind[this];
};
String.prototype.bind = function(bind, args){
var methodName = this;
if (Object.prototype.toString.call(args) != '[object Array]') args = [args];
return function(){
return (bind || this)[methodName].apply(bind || this, args || Array.prototype.slice.call(arguments));
};
};
String.prototype.pass = function(args, bind){
return this.bind(bind, args);
};
// Existing way…
this.element.getElement('.foo').tween.pass(['background-color', '#F00'], this.element.getElement('.foo'))
// Which is stupid, so you'd do…
var el = this.element.getElement('.foo'); el.tween.pass(['background-color', '#F00'], el)
// Which is also stupid, so you'd actually do…
function(){ this.element.getElement('.foo').tween('background-color', '#F00'); }.bind(this)
// Or…
var self = this; function(){ self.element.getElement('.foo').tween('background-color', '#F00'); }
// It would be much simpler like this…
'tween'.pass(['background-color', '#F00'], this.element.getElement('.foo'))
// Or even something like this…
'tween'.pass(['background-color', '#F00'])
// Since events fn.apply with the element by default
// etc…
// Lame :P
el.tween('height', 50).chain(
function(){ this.start('width',100) },
function(){ this.start('color','#f00') }
);
// Awesomeness, this haz it!
el.tween('height', 50).chain(
'start'.pass(['width',100]),
'start'.pass(['color','#f00'])
);
// Makes sense for simple stuff
el.addEvents({
mouseenter: 'tween'.pass(['background-color', '#f00']),
mouseleave: 'tween'.pass(['background-color', '#00f'])
});
// or
el.addEvents({
mouseenter: 'addClass'.pass('on'),
mouseleave: 'removeClass'.pass('on')
});
// but for more complex stuff it's best to keep it asis
el.addEvents({
mouseenter: function(){
this.tween('background-color', '#f00').addClass('on');
},
mouseleave: function(){
this.tween('background-color', '#00f').removeClass('on');
}
});
// etc…
el.addEvent('click', 'fade'.pass('out', differentElement));
'set'.call(el, 'text', 'Hello World!');
'set'.apply(el, ['text', 'Hello World!']);
'innerHTML'.apply(el);
['innerHTML', 'firstChild'].call(el);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment