以下是Crockford的JavaScript:The Good Parts的代码片段:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Run Code Online (Sandbox Code Playgroud)
克罗克福德继续解释
"通过使用方法方法扩充Function.prototype,我们不再需要输入原型属性的名称.现在可以隐藏这一点丑陋."
我基本上不知道这一点.在我们现在不再需要做什么之前我们必须做什么?
他说不是写作:
MyType.prototype.myMethod = function() {
..
};
Run Code Online (Sandbox Code Playgroud)
你可以这样写:
MyType.method("myMethod", function() {
...
});
Run Code Online (Sandbox Code Playgroud)
有可能(给定return this)点链另一个电话:
MyType.method("method1", function() {
...
}).method("method2", function() {
...
});
Run Code Online (Sandbox Code Playgroud)
咩.
| 归档时间: |
|
| 查看次数: |
128 次 |
| 最近记录: |