简体   繁体   English

在JavaScript中隐式添加构造函数原型的方法

[英]Implicitly add methods to constructor's prototype in JavaScript

The following is a code snippet from Crockford's JavaScript: The Good Parts : 以下是Crockford的JavaScript:The Good Parts的代码片段:

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

Crockford goes onto explain that 克罗克福德继续解释

"By augmenting Function.prototype with a method method, we no longer have to type the name of the prototype property. That bit of ugliness can now be hidden." “通过使用方法方法扩充Function.prototype,我们不再需要输入原型属性的名称。现在可以隐藏这一点丑陋。”

I'm basically at a loss to understand this point. 我基本上不知道这一点。 What did we have to do before that we no longer have to do now? 在我们现在不再需要做什么之前我们必须做什么?

He's saying that instead of writing: 他说不是写作:

MyType.prototype.myMethod = function() {
    ..
};

you can write this: 你可以这样写:

MyType.method("myMethod", function() {
    ...
});

with the possibility (given the return this ) of dot-chaining another call: 有可能(给予return this )点链另一个电话:

MyType.method("method1", function() {
    ...
}).method("method2", function() {
    ...
});

Meh. 咩。

Code break down: 代码分解:

Function.prototype.method

Every object in JavaScript allows prototypes . JavaScript中的每个对象都允许原型 It's JavaScript way of inheritance. 它是JavaScript的继承方式。 Function is the master object from which all other functions are created. Function是创建所有其他函数的master object When you set a prototype on it all Function objects inherit that property. 在其上设置原型时,所有Function对象都继承该属性。 In this case the function method . 在这种情况下的功能method

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

If you wanted the expand your own functions with other methods you need to type: 如果您希望使用其他方法扩展自己的functions ,则需要键入:

  somefunction.prototype.name = function(){}

With this method you can do just this: 使用此方法,您可以这样做:

  somefunction.method(name, function)

This funky bit does that: 这个时髦的一点就是这样:

  this.prototype[name] = func;

It refers to the Function object using the this keyword. 它使用this关键字引用Function对象。 The proceeds to the prototype for inheritance. 继承原型的收益。 Sets the function to the prototype using [name] . 使用[name]将函数设置为原型。 This converts the string name to a property. 这会将字符串名称转换为属性。 Why so? 为什么这样? Because: 因为:

 this.prototype.name 

would not work properly. 不会正常工作。 If you use the code above, every time a new method is added it will be referenced by name and not the name you'sve chosen. 如果您使用上面的代码,每次添加新method ,它都将按name引用,而不是您选择的名称。

Why is prototype[example] the same as prototype.example. 为什么prototype[example]与prototype.example相同。 Because in JavaScript every property is stored in a arrayish list on the object and can be called like you call an item from an array using Object[]. 因为在JavaScript中,每个属性都存储在对象的数组列表中,并且可以像使用Object []从数组中调用项目一样进行调用。

Lets prove corckford's statement: 让我们证明corckford的声明:

"By augmenting Function.prototype with a method method, we no longerhave to type the name of the prototype property. That bit of ugliness can now be hidden." “通过使用方法方法扩充Function.prototype,我们不再需要输入原型属性的名称。现在可以隐藏这一点丑陋。”

Here crockford wanted to say you can manipulate Function.prototype to achieve many different functionality for your personal usage.It is a functionality added to the function.prototype to add new method/property to any function.Functions in javascript inherits form Function.prototype.lets break down your code. 在这里,crockford想说你可以操作Function.prototype来为你的个人用途实现许多不同的功能。它是一个添加到function.prototype的功能,用于向任何函数添加新的方法/属性.javascript中的函数继承形式Function.prototype。让我们分解您的代码。

Function.prototype.method = function (name, func) { Function.prototype.method = function(name,func){

in this line Function.prototype is added a new method.This method receive two arguments. 在这行中为Function.prototype添加了一个新方法。这个方法接收两个参数。 name satands for a new methods name .And func stands for its functionality.I think you are familiar with what is methods and properties are.Functions are first class object in javascript.So they can be added new properties and methods during runtime. name satands的新方法名称。并且func代表它的功能。我认为你熟悉什么是方法和属性。函数是javascript中的第一类对象。所以它们可以在运行时添加新的属性和方法。

this.prototype[name] = func; this.prototype [name] = func;

Here this refers to the function which calls it. 这里是指调用它的功能。 [name] is method name and func is the method's functionality It adds a new method to passed function using array notation. [name]是方法名称, func是方法的功能它使用数组表示法为传递的函数添加一个新方法 Then finally 然后最后

return this; 归还这个;

with this statement passed function is returned with a new method added to it. 使用此语句传递函数返回时添加了一个新方法。

Here i have a practical example for you: 在这里,我有一个实际的例子:

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

function Myfunction(){       // a constructor function called Myfunction
    this.name='robin';      // it has a name property
}

Myfunction.method('mymethod',function (){ console.log(' i am available')});  //adds a method named 'mymethod to Myfunction constructor

var newObj=new Myfunction();     // creates a new instance  of Myfunction class
console.log(newObj.mymethod());  //calls mymethod() of newObj

Here myfunction is a constructor.We can add inherited methods to this constructor's prototype usind Myfunction.prototype.mymethod=function(){ ..} 这里myfunction是一个构造函数。我们可以将继承的方法添加到这个构造函数的原型usind Myfunction.prototype.mymethod=function(){ ..}

If you use myfunction as a constructor this is the way you add method to it.But As you've added a new functionality to Function.prototype ,you can simply call it like 如果你使用myfunction作为构造函数,这就是你向它添加方法的方法。但是当你为Function.prototype添加了一个新功能时,你可以简单地调用它

Myfunction.method('mymethod',function (){console.log('i am available'}); //it will add a method named mymethod to Myfunction's prototype 

This statement will add a new method called mymethod to Myfunction()'s prototype implicitly . 这个语句将隐式地Myfunction()'s prototype添加一个名为mymethod的新方法。

so you don't have to write Myfunction.prototype.mymethod=function(){ console.log('i am available'); 所以你不必编写Myfunction.prototype.mymethod=function(){ console.log('i am available'); .Instead you can write Myfunction.method('mymethod',function (){ console.log('i am available'}); 。相反,你可以写Myfunction.method('mymethod',function (){ console.log('i am available'});

So it proves it will relieve you from writing Myfunction.prototype again and again every time you want to add new methods to Myfunction constructor . 所以它证明每次你想要向Myfunction构造函数添加新方法时,它会一次又一次地编写Myfunction.prototype

So it proves crockford's statement . 所以它证明了克罗克福德的说法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM