More effecient Function.prototype.bind and unit tests for bind, getById, and toArray.

This commit is contained in:
kangax 2010-09-06 12:33:57 -04:00
parent d3de320b49
commit d00a37d593
2 changed files with 49 additions and 4 deletions

View file

@ -1,8 +1,8 @@
if (!Function.prototype.bind) {
Function.prototype.bind = function(thisArg) {
var fn = this, args = Array.prototype.slice.call(arguments, 1);
return function() {
return fn.apply(thisArg, args.concat(Array.prototype.slice.call(arguments)));
};
var fn = this, args = slice.call(arguments, 1);
return args.length
? function() { return fn.apply(thisArg, args.concat(slice.call(arguments))) };
: function() { return fn.apply(thisArg, arguments) };
};
}

View file

@ -149,4 +149,49 @@
notEqual(obj, clone);
equals(clone.y, obj.y);
});
test('bind', function() {
ok(typeof Function.prototype.bind == 'function');
var obj = { };
function fn() {
return [this, arguments[0], arguments[1]];
}
var bound = fn.bind(obj);
same([obj, undefined, undefined], bound());
same([obj, 1, undefined], bound(1))
same([obj, 1, null], bound(1, null));
bound = fn.bind(obj, 1);
same([obj, 1, undefined], bound());
same([obj, 1, 2], bound(2));
});
test('getById', function() {
ok(typeof fabric.util.getById == 'function');
var el = document.createElement('div');
el.id = 'foobarbaz';
document.body.appendChild(el);
equals(el, fabric.util.getById(el));
equals(el, fabric.util.getById('foobarbaz'));
equals(null, fabric.util.getById('likely-non-existent-id'));
});
test('toArray', function() {
ok(typeof fabric.util.toArray == 'function');
same(['x', 'y'], fabric.util.toArray({ 0: 'x', 1: 'y', length: 2 }));
same([1, 3], fabric.util.toArray(function(){ return arguments }(1, 3)));
var nodelist = document.getElementsByTagName('div'),
converted = fabric.util.toArray(nodelist);
ok(converted instanceof Array);
equals(nodelist.length, converted.length);
equals(nodelist[0], converted[0]);
equals(nodelist[1], converted[1]);
});
})();