Create #alias method, callable on any object, _ by default.

While I'm not a fan of making abstractions where a simpe solution
exists, I think this is good as it makes a public API for extending
underscore, therefore making it "ok" to make your own alias names.

Also give us some future proofing in case we ever add in hooks on
method definition. For example, right now if you add a method
or make an alias in user code, it isn't added to the wrapper prototype.

Added tests and inline docs, no external docs
This commit is contained in:
Mike Frawley
2010-02-17 09:41:18 -06:00
parent c4daac089b
commit c43de549ba
2 changed files with 50 additions and 10 deletions

View File

@@ -11,7 +11,7 @@ $(document).ready(function() {
});
test("objects: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact",
var expected = ["alias", "all", "any", "bind", "bindAll", "breakLoop", "clone", "compact",
"compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual",
@@ -183,4 +183,19 @@ $(document).ready(function() {
value();
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
});
test("objects: alias", function() {
_.alias('isEqual', 'isTheSame');
ok(_.isTheSame(9, 9), 'by default aliases methods on underscore');
delete _.isTheSame;
//
var o = { hi: function () {return 9;} };
_.alias(o, 'hi', 'ho');
equals(o.ho(), 9, 'can add an alias on another object');
_.alias(o, 'hi', 'there', 'sir');
ok(o.hi==o.sir, 'can add multiple aliases');
});
});