Javascript 增强基本类型(原型继承)
我刚刚开始阅读 Douglas Crockford 的“Javascript The Good parts”,其中他解释了增强基本类型。
Function.prototype.addMethod=function(name,func) {
this.prototype[name]=func;
return this;
};
执行此操作后,addMethod 就可用于所有基本对象,例如 String、Number 等。我很困惑
为什么当我没有将其添加到Object.prototype时会发生这种情况?
为什么向 Function.prototype 添加方法会反映在所有基本对象中?
I just started reading on Douglas Crockford's "Javascript The Good parts" where he explains about Augmenting basic types.
Function.prototype.addMethod=function(name,func) {
this.prototype[name]=func;
return this;
};
The moment after doing this, the addMethod becomes available for all basic objects like String, Number etc. That leaves me puzzled
Why does this happen when I haven't added it to Object.prototype?
Why adding a method to Function.prototype gets reflected in all basic objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他可能的意思是,执行此操作后,addMethod 对所有基本
对象对象类型(例如 String、Number 等)可用。这是因为 String 对象是一个函数(但是String 创建的对象不是)。例如,给定
You can do
but not
JavaScript 类型系统的简要说明如下:
JavaScript 没有正常的类概念。相反,您可以通过在调用函数时将 new 关键字放在函数前面,将任何函数转换为构造函数来实现相同的效果。
例如:
如果您调用它,
它将创建一个名为 myObj 的对象。该对象将有一个名为 myX 的成员变量。函数 MyFunction 被认为是对象的构造函数(并存储在“构造函数”属性中。
(额外问题:如果您在不使用 new 关键字的情况下调用上面的函数,即
var x = MyFunction(10) 会发生什么)答案可能会让任何明智的人感到惊讶。)
现在您已经看到了如何将任意函数转换为构造函数,内置对象是完全相同的,字符串对象是由函数 String、数字创建的。是由函数 Number 创建的,等等。
这些内置对象是由函数创建的一样,每个函数也是由“Function”函数创建的(哎呀!)
就像
。你在某处编写
由 MyFunction 构造函数/函数创建的所有对象似乎都有一个名为 someNewMethod 的额外成员函数。你可以使用原型做许多其他时髦的事情,例如替换原型,或替换原型的原型,但我不是一个。这方面的专家。
He probably meant The moment after doing this, the addMethod becomes available for all basic
objectsobject types like String, Number etc. This is because the String object is a function (but objects created by String are not).E.g., given
You can do
but not
A brief explanation of the JavaScript type system comes here:
JavaScript does not have the normal concept of classes. Instead, you can achieve somewhat the same by turning any function into a constructor by puttin the new keyword in front of it when it is invoked.
E.g: given
if you invoke it like
it will create an object called myObj. This object will have a single member variable called myX. The function MyFunction is considered the constructor of the object (and is stored in the "constructor" property.
(Bonus question: What will happen if you invoke the function above without the new keyword, i.e.
var x = MyFunction(10)
. The answer will probably surprise any sensible person.)Now you have seen how any arbitrary function can be turned into a constructor. The built-in objects are exactly the same, string objects are created by the function String, numbers are created by the function Number, etc.
Just as those built-in objects are created by functions, each of those functions are also created by the "Function" function (yikes!).
Now on to prototypes.
in the example above, if you somewhere write
all objects created by the MyFunction constructor/function will appear to have an extra member function called someNewMethod. You can do many other funky things with prototypes, like replacing the prototype, or replacing the prototype's prototype, but I'm not an expert at that.
在面向对象的 javascript 中,函数可以充当类和类。构造函数。因此,如果您的类名为 MyObject,您可以执行以下操作:
在您的示例中,addMethod 作为实例方法添加到 Function 类,这意味着它可用于 Function 的所有实例。 MyObject 函数/类/构造函数是 Function 的实例,因此您可以对其调用 addMethod 。这适用于大多数对象类型,但不适用于 IE 和其他一些浏览器中的 HTMLElements。
In object-oriented javascript, a function can serve as a class & constructor. So if your class was named MyObject, you could do the following:
In your example, the addMethod was added as an instance method to the Function class, which means it is available to all instances of Function. The MyObject function/class/constructor is an instance of Function, so you can call addMethod on it. This works with most any object-type, but not HTMLElements in IE and some other browsers.
JavaScript 中的函数是对象。所有对象都有一个隐藏链接
到 Object.prototype。因此对于第一个声明:
<前><代码>> `Function.prototype.addMethod=函数(名称,func){}
您已经声明了一个链接到 Function.prototype 的函数,而 Function.prototype 本身又链接到 Object.prototype。
Functions in JavaScript are objects. All objects have a hidden link
to Object.prototype. Thus for the first declaration:
You have declared a function that is linked to Function.prototype which itself is linked to Object.prototype.