我有一个小时的空闲时间,所以我想我会尝试实施我从“胡安门德斯”和“系统”收到的各种建议。
以下只是一个工作示例(未优化、未进行基准测试、未清理),因此请认为它正在进行中。在我做任何其他事情之前,我想运行一些基准测试,看看它最终会提供什么样的“膨胀”。
如果有人认为这有什么问题,请提供建设性的批评!
function Overload() {
// Empty for now!
}
Overload.prototype.link = function (object, method) {
console.log("Creating dispatch method '" + method + "()'");
object.prototype[method] = function () {
var target = method + "_";
console.log("Invoked dispatch method '" + method + "()'...");
for (var i=0; i < arguments.length; i++) {
target += (typeof arguments[i]).substring(0, 1);
}
console.log("Resolved target as '" + target + "'");
if (typeof object.prototype._overloaded[target] !== "undefined") {
console.log("Dispatching to overloaded method: '" + target + "'");
return object.prototype._overloaded[target].apply(object, arguments);
} else {
console.log("Method not found: '" + method + "('" + target + "')'");
//throw "Exception ...";
}
}
}
以下函数将用于重载对象上的所有其他函数:
Overload.prototype.overload = function (object, method, fn, params) {
var target = method + "_" + params;
console.log("Overloading method '" + method + "()' to '" + method + "('" + params + "')'");
if (typeof object === "undefined") {
console.log("Object doesn't exist!");
}
if (typeof object.prototype[method] === "undefined") {
this.link(object, method);
}
if (typeof object.prototype._overloaded === "undefined") {
console.log("Creating '[obj].prototype._overloaded' property");
object.prototype._overloaded = {};
}
if (typeof object.prototype._overloaded[target] === "undefined") {
//console.log("Assigning overload function as target '" + method + "('" + params + "')'");
object.prototype._overloaded[target] = fn;
} else {
console.log("Definition for '" + method + "('" + params + "')' already eixsts!");
}
return fn;
}
现在定义一些本质上“模拟”重载函数的示例函数:
function fn1(one) {
console.log("Invoked function 1: " + one);
}
function fn2(one, two) {
console.log("Invoked function 2: " + one + ", " + two);
}
function fn3(one, two, three) {
console.log("Invoked function 3: " + one + ", " + two + ", " + three);
}
function fn4(one, two, three) {
console.log("Invoked function 4: " + one + ", " + two);
}
function fn5(one, two, three) {
console.log("Invoked function 5: " + one + ", " + two);
}
使用它来运行我们的测试:
function testMethodOverloading() {
console.log("Testing method overloading!");
var ov = new Overload();
function OBJ() {}
console.log("--");
ov.overload(OBJ, "name", fn1, 's');
ov.overload(OBJ, "name", fn2, 'sn');
ov.overload(OBJ, "name", fn3, 'sns');
ov.overload(OBJ, "name", fn4, 'ss');
ov.overload(OBJ, "name", fn5, 'nn');
console.log("--");
var obj = new OBJ();
obj.name("one");
obj.name("two", 1);
obj.name("three", 2, "four");
obj.name("five", "six");
obj.name(3, 4);
}
这是我从上面运行测试得到的输出:
Overloading method 'name()' to 'name('s')'
Creating dispatch method 'name()'
Creating '[obj].prototype._overloaded' property
Overloading method 'name()' to 'name('sn')'
Overloading method 'name()' to 'name('sns')'
Overloading method 'name()' to 'name('ss')'
Overloading method 'name()' to 'name('nn')'
--
Invoked dispatch method 'name()'...
Resolved target as 'name_s'
Dispatching to overloaded method: 'name_s'
Invoked function 1: one
Invoked dispatch method 'name()'...
Resolved target as 'name_sn'
Dispatching to overloaded method: 'name_sn'
Invoked function 2: two, 1
Invoked dispatch method 'name()'...
Resolved target as 'name_sns'
Dispatching to overloaded method: 'name_sns'
Invoked function 3: three, 2, four
Invoked dispatch method 'name()'...
Resolved target as 'name_ss'
Dispatching to overloaded method: 'name_ss'
Invoked function 4: five, six
Invoked dispatch method 'name()'...
Resolved target as 'name_nn'
Dispatching to overloaded method: 'name_nn'
Invoked function 5: 3, 4
所以它肯定会按照我的预期/想要的那样运行!但是它将获得什么样的基准。接下来我将这样做,看看我是否在引用文章的基准范围内(即在剥离所有日志记录之后)。一旦我有一个大致的想法,我会发布结果。