“方法” Crockford 书中的方法:Javascript: The Good Parts

发布于 2024-09-28 16:59:40 字数 2030 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风夜微凉 2024-10-05 16:59:40

JavaScript 中的所有本机函数都继承自 Function.prototypeNumberStringObjectArrayRegExp 都是函数,因此它们继承自Function.prototype

method 旨在在构造函数上调用。它的工作是将您提供给它的函数变成一个方法,该方法存在于您调用 method 的构造函数创建的每个对象中。您会注意到,在 Crockford 传递给 method 的函数中,他使用了 this,它是对调用该方法的对象的引用。 Array.dimArray.matrixArray.identity 不使用 this,因为它们的操作独立于任何特定数组,因此不需要是单个数组对象的方法。为了方便起见,它们被指定为 Array 函数的属性:它们同样可以作为全局作用域中的函数单独存在。

All native functions in JavaScript inherit from Function.prototype. Number, String, Object, Array and RegExp are all functions, therefore they inherit from Function.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 called method. You will notice that in the functions that Crockford passes to method, he makes use of this, which is a reference to the object on which the method was called. Array.dim, Array.matrix and Array.identity make no use of this 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 the Array function for convenience: they could equally well exist on their own as functions in the global scope.

错爱 2024-10-05 16:59:40

顺便说一句,在 P40 上:

end () 表示“使用该函数返回的函数”,而不是返回它的外部函数。

如果他省略了final(),对 deentityify 的调用将返回一个函数,而不是一个字符串。

用道格拉斯·克罗克福德自己的话说:

我们立即调用我们刚刚创建的函数
与 () 运算符一起使用。该调用创建并返回函数
这成为 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:

We immediately invoke the function we just made
with the () operator. That invocation creates and returns the function
that becomes the deentityify method.

找个人就嫁了吧 2024-10-05 16:59:40

@Tim Down 给出的解决方案是准确的,但并不完全清楚。

函数对象与函数实例对象

首先,在 javascript 中,函数也是对象。由此看来,我指的不是 new() 构造创建的对象,而是函数本身。为了避免混淆,我将此类对象称为函数对象,并将使用函数的 new () 构造创建的对象称为函数实例对象

_proto_和原型属性

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。

继承是什么意思?

这意味着任何继承对象都可以使用继承对象的任何属性。

什么是 Function.prototype

是每个函数对象_proto_所引用的对象。这意味着每个 Function 对象 都可以访问 Function.prototype 的属性,因为每个 Function 对象 都继承 Function.prototype 对象。这也意味着,如果将 method 属性添加到 Function.prototype 对象,则 javascript 中所有可能的 Function 对象 都可以使用该属性。这包括字符串、数字等。

this.prototype[name] = func;

当从函数对象(如数字、字符串等)调用“方法”时,这指的是函数对象。这意味着我们现在有了一个新属性在名为“name”的函数对象中,它是一个函数“func”。

Function 对象的 prototype 属性有什么好处

函数对象prototype是由使用该函数的新构造创建的函数实例对象 _ proto _ 引用。

如果执行以下命令:

Number.method('整数', function () {...});

那么 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.

Function object vs Function instance object

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.

_ proto _ and prototype properties

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

How is inheritance defined?

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.

What is meant by inheritance?

It means any inheriting object can use any property of inherited object.

What is Function.prototype

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.prototype[name] = func;

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'.

What good is prototype property of Function object

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:

Number.method('integer', function () {...});

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.

小矜持 2024-10-05 16:59:40

尝试使用这个原型方法:

String.prototype.deentityify = function () { ... }

那么:

document.writeln('<">'.deentityify( ));

我们可以看到:
<">

Try to use this prototype method:

String.prototype.deentityify = function () { ... }

Then:

document.writeln('<">'.deentityify( ));

We can see:
<">

ま昔日黯然 2024-10-05 16:59:40

示例:如果有人遇到困难,柯里化可以重写如下。请参阅 jsFiddle 演示

 Function.prototype.curry = function ( ) {
 var slice = Array.prototype.slice;
 var args = slice.apply(arguments);
 var that = this; 
 return function () {  
    return that.apply(null,args.concat (slice.apply(arguments)));
 }
};
var add = function(a, b)
{
    return a + b;
}
var add1 = add.curry(1);
document.writeln(add1(6)); // 7

Example: Currying can be rewritten as follows, if anyone got stuck. See jsFiddle demo

 Function.prototype.curry = function ( ) {
 var slice = Array.prototype.slice;
 var args = slice.apply(arguments);
 var that = this; 
 return function () {  
    return that.apply(null,args.concat (slice.apply(arguments)));
 }
};
var add = function(a, b)
{
    return a + b;
}
var add1 = add.curry(1);
document.writeln(add1(6)); // 7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文