道格拉斯·克罗克福德在他的书中写道(第4页):
整本书都使用一个名为
然后他开始使用这种方法在
P40(不确定第41页是否有印刷错误:the end()):
P47(我在这里有些困惑,不知道为什么Crockford定义了
P48:
第48页:
P90(不确定第91页中是否有打印错误:the end()):
整本书都使用一个名为
method
的方法来定义新方法,这是它的定义:Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
然后他开始使用这种方法在
Number、String、Function、Object、Array、RegExp
中添加方法,以下是完整列表:
P33:
Number.method('integer', function () {...});
String.method('trim', function () {...});
P40(不确定第41页是否有印刷错误:the end()):
String.method('deentityify', function () {...}());
P43和P44:
Function.method('curry', function () {...});
P47(我在这里有些困惑,不知道为什么Crockford定义了
new
方法,而他似乎从未在书中使用new
方法):Function.method('new', function () {...});
P48:
第48页:
Function.method('inherits', function (Parent) {...});
P54:
Object.method('superior', function (name) {...});
P62:
Array.method('reduce', function (f, value) {...});
P79:
Array.method('pop', function () {...});
Array.method('push', function () {...});
Array.method('shift', function () {...});
P82:
Array.method('splice', function (start, deleteCount) {...});
P84:
Function.method('bind', function (that) {...});
P88:
RegExp.method('test', function (string) {...});
String.method('charAt', function (pos) {...});
P90(不确定第91页中是否有打印错误:the end()):
String.method('entityify', function () {...}());
“方法”这个定义是基于“函数”的,为什么它可以在除了“函数”之外的数据类型中使用,例如“数字、字符串、对象、数组、正则表达式”?并且这个“方法”能用于其他数据类型吗?
另一个小问题:在第63页和64页中,Array.dim, Array.matrix, Array.identity
的定义没有使用上述的“方法”,为什么?
alert(typeof Number)
等方法,或者查看规范(第15.1.4节):http://www.ecma-international.org/publications/standards/Ecma-262.htm。如果你的意思是它们也是全局对象的属性,那么也是这样的。 - Tim DownFunction.prototype.method
时,它按预期工作。Number.method
被定义。但是当我在NodeJS中尝试时,Number.method
是undefined
。这就是我在阅读Crockford的书时尝试后引发这个问题的原因。 - dmmfll