Javascript 增强基本类型(原型继承)

发布于 2024-08-03 16:42:10 字数 494 浏览 3 评论 0原文

我刚刚开始阅读 Douglas Crockford 的“Javascript The Good parts”,其中他解释了增强基本类型。

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

执行此操作后,addMethod 就可用于所有基本对象,例如 StringNumber 等。我很困惑

  1. 为什么当我没有将其添加到Object.prototype时会发生这种情况?

  2. 为什么向 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

  1. Why does this happen when I haven't added it to Object.prototype?

  2. Why adding a method to Function.prototype gets reflected in all basic objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-08-10 16:42:10

他可能的意思是,执行此操作后,addMethod 对所有基本对象 对象类型(例如 String、Number 等)可用。这是因为 String 对象是一个函数(但是String 创建的对象不是)。

例如,给定

var s = '';

You can do

String.addMethod(...);

but not

s.addMethod(...);

JavaScript 类型系统的简要说明如下:

JavaScript 没有正常的类概念。相反,您可以通过在调用函数时将 new 关键字放在函数前面,将任何函数转换为构造函数来实现相同的效果。

例如:

function MyFunction(x) { this.myX = x; }

如果您调用它,

var myObj = new MyFunction(10);

它将创建一个名为 myObj 的对象。该对象将有一个名为 myX 的成员变量。函数 MyFunction 被认为是对象的构造函数(并存储在“构造函数”属性中。

(额外问题:如果您在不使用 new 关键字的情况下调用上面的函数,即 var x = MyFunction(10) 会发生什么)答案可能会让任何明智的人感到惊讶。)

现在您已经看到了如何将任意函数转换为构造函数,内置对象是完全相同的,字符串对象是由函数 String、数字创建的。是由函数 Number 创建的,等等。

这些内置对象是由函数创建的一样,每个函数也是由“Function”函数创建的(哎呀!)

就像

。你在某处编写

MyFunction.prototype.someNewMethod = function() {}

由 MyFunction 构造函数/函数创建的所有对象似乎都有一个名为 someNewMethod 的额外成员函数。你可以使用原型做许多其他时髦的事情,例如替换原型,或替换原型的原型,但我不是一个。这方面的专家。

He probably meant The moment after doing this, the addMethod becomes available for all basic objects object types like String, Number etc. This is because the String object is a function (but objects created by String are not).

E.g., given

var s = '';

You can do

String.addMethod(...);

but not

s.addMethod(...);

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

function MyFunction(x) { this.myX = x; }

if you invoke it like

var myObj = new MyFunction(10);

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

MyFunction.prototype.someNewMethod = function() {}

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.

箜明 2024-08-10 16:42:10

在面向对象的 javascript 中,函数可以充当类和类。构造函数。因此,如果您的类名为 MyObject,您可以执行以下操作:

// create a class/constructor
function MyObject() {
   // ...
}

// add a static method to the MyObject class
MyObject.someFunction = function() {
   // ...
}

// add an instance method to the MyObject Class
MyObject.prototype.someFunction = function() {
   // ...
}

在您的示例中,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:

// create a class/constructor
function MyObject() {
   // ...
}

// add a static method to the MyObject class
MyObject.someFunction = function() {
   // ...
}

// add an instance method to the MyObject Class
MyObject.prototype.someFunction = function() {
   // ...
}

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.

是伱的 2024-08-10 16:42:10
  1. JavaScript 中的函数是对象。所有对象都有一个隐藏链接
    到 Object.prototype。因此对于第一个声明:

    <前><代码>> `Function.prototype.addMethod=函数(名称,func){}

    您已经声明了一个链接到 Function.prototype 的函数,而 Function.prototype 本身又链接到 Object.prototype。

  2. 对于第二部分,它只是一个赋值,您将名称值对设置为 Object.prototype 并将 add 方法返回到所有 String、Number 对象,因为它们都是在原型中声明的。
  1. Functions in JavaScript are objects. All objects have a hidden link
    to Object.prototype. Thus for the first declaration:

    > `Function.prototype.addMethod=function(name,func) {}
    

    You have declared a function that is linked to Function.prototype which itself is linked to Object.prototype.

  2. For the second part its just an assignment where you are setting the name value pair to the Object.prototype and will return the add method to all String, Number objects since it is all declared in the prototype.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文