简体   繁体   English

我什么时候应该在JavaScript中使用“prototype”

[英]When should I use “prototype” in JavaScript

I have done a fair amount of programming in JavaScript/JQuery. 我在JavaScript / JQuery中做了大量的编程。

But I never used "prototype". 但我从未使用过“原型”。 In fact, I have no clue what it means. 事实上,我不知道这意味着什么。

Do you have a practical example when it is useful? 当它有用时,你有一个实际的例子吗?

Simplest example: 最简单的例子:

function Foo() {
    this.bar = function() {
        return 42;
    }
}

Now you can create as many instances of Foo as you want and call bar() : 现在您可以根据需要创建任意数量的Foo实例并调用bar()

var a = new Foo();
var b = new Foo();

Even though both object have bar() method which is exactly the same in both cases, these are distinct methods. 尽管两个对象的bar()方法在两种情况下都完全相同,但这些方法都是不同的。 In fact, each new Foo object will have a new copy of this method. 实际上,每个新的Foo对象都将拥有此方法的新副本。

On the other hand: 另一方面:

function Foo() {}
Foo.prototype.bar = function() {
    return 42;
}    

has the same end result but the function is stored only once in object prototype rather than in an object itself. 具有相同的最终结果,但该函数仅在对象prototype存储一次而不是在对象本身中存储。 This may be a deal breaker if you create tons of Foo instances and want to save some memory. 如果您创建大量的Foo实例并希望节省一些内存,这可能是一个交易破坏者。

Assuming you are asking about Object.prototype, 假设你问的是Object.prototype,

All objects in JavaScript are descended from Object; JavaScript中的所有对象都来自Object; all objects inherit methods and properties from Object.prototype, although they may be overridden. 所有对象都从Object.prototype继承方法和属性,尽管它们可能被覆盖。 For example, other constructors' prototypes override the constructor property and provide their own toString methods. 例如,其他构造函数的原型覆盖构造函数属性并提供自己的toString方法。 Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain. 对原型对象的更改将传播到所有对象,除非沿着原型链进一步覆盖受这些更改影响的属性和方法。

Read this and then probably this 这个 ,然后可能这个

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

相关问题 我应该使用原型吗? - Should I use prototype or not? 什么时候应该在javascript中的对象扩充期间使用“原型”? - When should you use “prototype” during object augmentation in javascript? 在javascript中声明名称空间时应使用原型吗 - Should you use prototype when declaring a namespace in javascript Javascript粒子:我应该使用工厂模式还是对象/原型模式? - Javascript particles: should I use a factory or an object/prototype pattern? Require.js:扩展模块时可以直接使用原型还是应该使用扩展? - Require.js: When extending a module can I use prototype directly or I should use extend as well? 我什么时候应该在JavaScript中使用MVC框架? - When should I use an MVC framework in JavaScript? 我什么时候应该使用javascript框架库? - When should I use a javascript framework library? 我什么时候应该在 Javascript 中将函数用作类? - When should I use functions as classes in Javascript? 是否值得使用原型或我们应该使用OOP的JavaScript? - Is it worth to use prototype or should we use OOP for javascript? 要将原型分配给Number,我应该使用Function.prototype还是Object.prototype? - To assign a prototype to Number, should I use Function.prototype or Object.prototype?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM