Joh*_*ohn 23 javascript methods
道格拉斯·克罗克福德在他的书(第4页)中写道:
在整本书中,一个method方法用于定义新方法,这是它的定义:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Run Code Online (Sandbox Code Playgroud)
然后他开始使用它method来添加方法Number, String, Function, Object, Array, RegExp,这里是完整的列表:
P33:
Number.method('integer', function () {...});
String.method('trim', function () {...});
Run Code Online (Sandbox Code Playgroud)
P40(不确定第41页是否存在印刷错误:结束()):
String.method('deentityify', function () {...}());
Run Code Online (Sandbox Code Playgroud)
P43和P44:
Function.method('curry', function () {...});
Run Code Online (Sandbox Code Playgroud)
P47(我在这里很困惑,不知道为什么Crockford定义new方法,而且他似乎从不new在书中使用方法):
Function.method('new', function () {...});
Run Code Online (Sandbox Code Playgroud)
P48:
Function.method('inherits', function (Parent) {...});
Run Code Online (Sandbox Code Playgroud)
P54:
Object.method('superior', function (name) {...});
Run Code Online (Sandbox Code Playgroud)
P62:
Array.method('reduce', function (f, value) {...});
Run Code Online (Sandbox Code Playgroud)
P79:
Array.method('pop', function () {...});
Array.method('push', function () {...});
Array.method('shift', function () {...});
Run Code Online (Sandbox Code Playgroud)
P82:
Array.method('splice', function (start, deleteCount) {...});
Run Code Online (Sandbox Code Playgroud)
P84:
Function.method('bind', function (that) {...});
Run Code Online (Sandbox Code Playgroud)
P88:
RegExp.method('test', function (string) {...});
String.method('charAt', function (pos) {...});
Run Code Online (Sandbox Code Playgroud)
P90(不确定页面91中是否有错误打印:结束()):
String.method('entityify', function () {...}());
Run Code Online (Sandbox Code Playgroud)
该定义method是基于Function,为什么它可以用于Number, String, Object, Array, RegExp除Function?这method可以用于其他数据类型吗?
另一个小问题:在第63页和第64页中,定义Array.dim, Array.matrix, Array.identity没有使用上面method,为什么?
Tim*_*own 22
JavaScript中的所有本机函数都继承自Function.prototype.Number,String,Object,Array和RegExp是所有的功能,因此它们继承Function.prototype.
method旨在调用构造函数.它的工作是将你提供给它的函数变成一个方法,该方法存在于你调用的构造函数创建的每个对象method.你会注意到在Crockford传递给的函数中method,他使用了this,这是对调用该方法的对象的引用.Array.dim,Array.matrix而Array.identity空耗this,因为他们任何特定阵列的独立运行,因此不需要是单个阵列对象的方法.Array为方便起见,它们被指定为函数的属性:它们同样可以作为全局范围内的函数自行存在.
| 归档时间: |
|
| 查看次数: |
4415 次 |
| 最近记录: |