“方法” Crockford 书中的方法:Javascript: The Good Parts
Douglas Crockford 在他的书(第 4 页)中写道:
在整本书中,一个 method
方法用于定义新方法,这是它的定义:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
然后他开始使用这个 method
> 在Number, String, Function, Object, Array, RegExp
中添加方法,完整列表如下:
P33:
Number.method('integer', function () {...});
String.method('trim', function () {...});
P40 (不确定第41页是否有印刷错误:end() ):
String.method('deentityify', function () {...}());
P43 & P44:
Function.method('curry', function () {...});
P47(我在这里很困惑,不知道为什么Crockford定义了new
方法,而且他在书中似乎从来没有使用过new
方法):
Function.method('new', function () {...});
P48:
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页是否有印刷错误:结尾()):
String.method('entityify', function () {...}());
定义方法
是基于函数,为什么除了
Function
之外,还可以用在Number、String、Object、Array、RegExp
中?而且这个
方法
可以吗?用于其他数据类型?
另一个小问题:在第63页& 64、Array.dim、Array.matrix、Array.identity
的定义没有使用上面的方法
,为什么?
Douglas Crockford wrote in his book (Page 4):
Throughout the book, a method
method is used to define new methods, This is its definition:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Then he starts to use this method
to add method in Number, String, Function, Object, Array, RegExp
, and here is the complete list:
P33:
Number.method('integer', function () {...});
String.method('trim', function () {...});
P40 (not sure if there is a misprint in Page 41: the end () ):
String.method('deentityify', function () {...}());
P43 & P44:
Function.method('curry', function () {...});
P47 (I am confused here, don't know why Crockford define new
method, and he seems never use new
method in the book):
Function.method('new', function () {...});
P48:
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 (not sure if there is a misprint in Page 91: the end () ):
String.method('entityify', function () {...}());
The definition method
is based on Function
, why it can be used in Number, String, Object, Array, RegExp
besides Function
? And can this method
be used to other data type?
Another small question: in Page 63 & 64, the definition of Array.dim, Array.matrix, Array.identity
didn't use above method
, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
JavaScript 中的所有本机函数都继承自
Function.prototype
。Number
、String
、Object
、Array
和RegExp
都是函数,因此它们继承自Function.prototype
。method
旨在在构造函数上调用。它的工作是将您提供给它的函数变成一个方法,该方法存在于您调用method
的构造函数创建的每个对象中。您会注意到,在 Crockford 传递给method
的函数中,他使用了this
,它是对调用该方法的对象的引用。Array.dim
、Array.matrix
和Array.identity
不使用this
,因为它们的操作独立于任何特定数组,因此不需要是单个数组对象的方法。为了方便起见,它们被指定为 Array 函数的属性:它们同样可以作为全局作用域中的函数单独存在。All native functions in JavaScript inherit from
Function.prototype
.Number
,String
,Object
,Array
andRegExp
are all functions, therefore they inherit fromFunction.prototype
.method
is intended to be called on constructor functions. Its job is to make the function you supply to it into a method that exists for every object created by the constructor function on which you calledmethod
. You will notice that in the functions that Crockford passes tomethod
, he makes use ofthis
, which is a reference to the object on which the method was called.Array.dim
,Array.matrix
andArray.identity
make no use ofthis
because they operate independently of any particular array and hence do not need to be methods of individual array objects. They are assigned as properties of theArray
function for convenience: they could equally well exist on their own as functions in the global scope.顺便说一句,在 P40 上:
end () 表示“使用该函数返回的函数”,而不是返回它的外部函数。
如果他省略了final(),对 deentityify 的调用将返回一个函数,而不是一个字符串。
用道格拉斯·克罗克福德自己的话说:
As an aside, on P40:
The end () means "use the function that this function returns", not the outer function that returns it.
If he had left off the final (), a call to deentityify would return a function, rather than a string.
In Douglas Crockford's own words:
@Tim Down 给出的解决方案是准确的,但并不完全清楚。
首先,在 javascript 中,函数也是对象。由此看来,我指的不是 new() 构造创建的对象,而是函数本身。为了避免混淆,我将此类对象称为函数对象,并将使用函数的 new () 构造创建的对象称为函数实例对象。
javascript 中的任何函数对象 都有两个属性: _ proto _ 和prototype。此外,任何Function实例对象(使用new构造函数创建)都有一个属性 _ proto _ 。 _ proto _ 定义了继承。 找到
一些关于这方面的好资源可以在http://zeekat.nl/articles/ 构造函数-considered-mildly-confusing.html
如果 objA 和 objC 通过任意数量的 _ proto _ 连接,则对象 objA 继承另一个对象 objC。因此,如果 objA 的 _ proto _ 等于 objB,并且 objB 的 _ proto _ 等于 objC,则 objA 继承 objB 和 objC,而 objB 继承 objC。
这意味着任何继承对象都可以使用继承对象的任何属性。
是每个函数对象的_proto_所引用的对象。这意味着每个 Function 对象 都可以访问 Function.prototype 的属性,因为每个 Function 对象 都继承 Function.prototype 对象。这也意味着,如果将 method 属性添加到 Function.prototype 对象,则 javascript 中所有可能的 Function 对象 都可以使用该属性。这包括字符串、数字等。
当从函数对象(如数字、字符串等)调用“方法”时,这指的是函数对象。这意味着我们现在有了一个新属性在名为“name”的函数对象中,它是一个函数“func”。
函数对象的prototype是由使用该函数的新构造创建的函数实例对象的 _ proto _ 引用。
如果执行以下命令:
那么 Number.prototype 中定义了 integer 方法。这意味着每个 Number函数实例对象,例如 new Number (2.4),都会从 Number.prototype“继承”这个新属性“integer”,因为该 Number函数实例对象会将其 _ proto _ 设置为 Number.prototype。
The solution as given by @Tim Down is accurate, but not completely clear.
First of all, in javascript, a function is also an object. From this, I mean not the object created by new () construct, but the function itself. To avoid confusion, I would refer such objects as Function object, and for object created using new () construct of a function as Function instance object.
Any function object in javascript has two properties: _ proto _ and prototype. Moreover, any Function instance object (created using new constructor) has a property _ proto _ . The _ proto _ is what defines the inheritance. Some good resource on this could be found at
http://zeekat.nl/articles/constructors-considered-mildly-confusing.html
An object objA inherits another object objC if objA and objC are connected through any number of _ proto _ . So if objA has _ proto _ equal to objB, and objB has _ proto _ equal to objC, then objA inherits objB and objC, while objB inherits objC.
It means any inheriting object can use any property of inherited object.
It is the object whom _ proto _ of every function object refers. This means every Function object has access to properties of Function.prototype, since every Function object inherits Function.prototype object. This also means that if method property is added to Function.prototype object, it would be available to all the possible Function objects in javascript. This includes Strings, Number, etc.
this refers to Function object when the 'method' is invoked from Function objects like Number, String, etc. Which means that we now have a new property in Function object with name "name", and its a function 'func'.
A function object's prototype is referred to by the Function instance object's _ proto _ created using that function's new construct.
If the following was executed:
then Number.prototype has that integer method defined in it. This means each Number function instance object, e.g. new Number (2.4), would "inherit" this new property 'integer' from Number.prototype, since that Number function instance object would have its _ proto _ set to Number.prototype.
尝试使用这个原型方法:
那么:
我们可以看到:
<">
Try to use this prototype method:
Then:
We can see:
<">
示例:如果有人遇到困难,柯里化可以重写如下。请参阅 jsFiddle 演示
Example: Currying can be rewritten as follows, if anyone got stuck. See jsFiddle demo