"方法"克罗克福德书中的方法:Javascript:The Good Parts [英] "method" method in Crockford's book: Javascript: The Good Parts

查看:125
本文介绍了"方法"克罗克福德书中的方法:Javascript:The Good Parts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道格拉斯·克罗克福德在他的书(第4页)中写道:



在整本书中,方法方法是用于定义新方法,这是它的定义:

  Function.prototype.method = function(name,func){
this.prototype [name] = func;
返回此;
};

然后他开始使用这个方法来在数字,字符串,函数,对象,数组,RegExp 中添加方法,这里是完整列表:



P33:

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

P40(不确定第41页是否存在印刷错误:结束()):

  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(){...}()); 

定义方法基于在函数,除了 Number,String,Object,Array,RegExp >功能?这个方法可以用于其他数据类型吗?



另一个小问题:第63页& 64, Array.dim,Array.matrix,Array.identity 的定义没有使用上面的方法,为什么?

解决方案

JavaScript中的所有本机函数都继承自 Function.prototype 数字字符串对象 Array RegExp 都是函数,因此它们继承自 Function.prototype



方法旨在在构造函数上调用。它的工作是将你提供给它的函数变成一个方法,该方法存在于你调用方法的构造函数创建的每个对象上。您会注意到在Crockford传递给方法的函数中,他使用这个,这是对调用该方法的对象。 Array.dim Array.matrix Array.identity 不使用这个,因为它们独立于任何特定数组运行,因此不需要是单个数组对象的方法。为方便起见,它们被指定为 Array 函数的属性:它们本身也可以作为全局范围内的函数存在。


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?

解决方案

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.

这篇关于"方法"克罗克福德书中的方法:Javascript:The Good Parts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆