JavaScript - 好的部分:函数原型与对象原型 [英] JavaScript - The Good Parts: Function prototypes vs Object prototypes

查看:92
本文介绍了JavaScript - 好的部分:函数原型与对象原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚读完JavaScript:The Good Parts - 好书。但我对第33-34页上的一个非常重要的主题感到困惑 - 增加类型。它描述了添加到Function.prototype的新方法的创建,因此当使用新方法调用时,所有函数都将具有该方法。很公平。但随后的示例显示了此方法正在使用Numbers和Strings。我想,这是对象 - 而不是功能。我在这里缺少什么?

Just finished reading "JavaScript: The Good Parts" - Great book. But I am confused about a very important topic on page 33-34 - Augmenting types. It describes the creation of a new method added to Function.prototype, so when invoked with a new method, all Functions will have that method available. Fair enough. But the subsequent examples show this method being used on Numbers and Strings. Which, I am thinking, are Objects - not Functions. What am I missing here?

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

用法示例:

Number.method('integer', function() {
    return Math[this < 0 ? 'ceiling' : 'floor'](this);
});

document.writeln((-10 / 3).integer()); //-3


推荐答案

您将方法添加到数字原型,因此每个Number 实例都可以访问它。换句话说,因为在Number原型上有一个名为integer的属性,所以从任何Number实例访问该属性的任何尝试都将成功。这就是将属性放在构造函数原型上的重点。

You add a method to the Number prototype, so every Number instance has access to it. In other words, because there's a property called "integer" on the Number prototype, any attempt to access that property from any Number instance will succeed. That's kind-of the whole point of putting properties on a constructor prototype.

当一个JavaScript原始数字出现在的左侧时。运算符,该语言会自动将其放入Number实例中,以便方法调用有意义。

When a JavaScript primitive number appears on the left side of a . operator, the language automatically boxes it in a Number instance so that the method call makes sense.

edit —让我们来看看方法功能的工作原理。在通话中

edit — let's look at how that "method" function works. In the call

Number.method( "integer", function() { ... } )

发生了什么事?好吧,在方法函数中,name参数是整数。然后将函数参数指定为 this.prototype 的属性。在方法函数的调用中,这个是什么?它是Number构造函数。因此,方法功能—它位于Function原型上,因此可用于所有函数实例,例如Number构造函数,例如—将给定函数添加为所涉及的构造函数原型的属性。

what's going on? Well, inside the "method" function, the "name" parameter is "integer". The function parameter is then assigned as a property of this.prototype. What is this in that invocation of the "method" function? It's the Number constructor function. Thus, the "method" function — which is on the Function prototype, and therefore is available to all function instances, like the Number constructor function for example — adds the given function as a property of the prototype of the constructor function involved.

为什么method属性作为Number构造函数的属性可见?因为Number构造函数本身就是一个函数。 method函数是作为Function原型的一个属性创建的,这意味着它对每个函数实例都是可见的—包括Number构造函数。

Why is the "method" property visible as a property of the Number constructor? Because the Number constructor is itself a function. The "method" function was created as a property of the Function prototype, so that means it is visible to every function instance — including the Number constructor.

这篇关于JavaScript - 好的部分:函数原型与对象原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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