“method” method in Crockford's book: [removed] The Good Parts

前端 未结 5 1636
不知归路
不知归路 2020-12-04 17:13

Douglas Crockford wrote in his book (Page 4):

Throughout the book, a method method is used to define new methods, This is its definition:



        
5条回答
  •  抹茶落季
    2020-12-04 17:42

    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.

提交回复
热议问题