javascript原型_JavaScript中的原型是什么?

javascript原型

Not to be confused with the JavaScript Prototype library, this post is going to be about prototypes themselves.

不要与JavaScript Prototype库混淆,本文将涉及原型本身。

A prototype is an object property that allows for objects to inherit common functionalities between each other. All JavaScript objects have the ability to inherit properties and methods from Object.prototype. Even prototypes themselves have the ability to inherit more properties, AKA another prototype, which is referred to as prototype chaining.

原型是一种对象属性,允许对象在彼此之间继承通用功能。 所有JavaScript对象都可以从Object.prototype继承属性和方法。 甚至原型本身也具有继承更多属性的能力,即另一个原型,也称为prototype chaining

Here is an example of a prototype being used:

这是使用原型的示例:

Prototypes are a very useful and powerful tool in programming. They enable you to add properties and define methods on all instances of a particular object all at once. When done in this way, it takes up less memory since it is only stored once on the prototype itself while still granting each instance access to it. If you instead decide to not use prototypes and add the methods to the constructor, each time that a new object instance is created, first it will have no knowledge of the properties prior to being created. And second, duplicate functions will be created, thus taking up more memory and eventually slowing your program down. As your application grows, the number of instances created will increase as well so using prototypes is ideal in keeping the costs down.

原型是编程中非常有用且功能强大的工具。 它们使您能够一次添加特定对象的所有实例的属性并定义方法。 通过这种方式完成后,它将占用较少的内存,因为它仅在原型本身上存储一次,同时仍允许每个实例对其进行访问。 如果您决定不使用原型,而是将方法添加到构造函数中,则每次创建新对象实例时,首先,它在创建之前将不了解属性。 其次,将创建重复的函数,从而占用更多的内存并最终减慢程序速度。 随着应用程序的增长,创建的实例数也会增加,因此使用原型是降低成本的理想选择。

There are a number of different methods already accessible to prototype objects which are useful tools that can be implemented. Some of these such methods are .toString(), .valueOf(), .isPrototypeOf(), etc.

原型对象已经可以使用许多不同的方法,这些方法是可以实现的有用工具。 其中一些方法是.toString() valueOf() .isPrototypeOf()等。

Object.create() (Object.create())

Object.create() is often used to create prototypes that can be explicitly defined. If you create a new object with the new keyword, you don’t have as much control over the object’s properties. The Object.create() function takes in two arguments: the first argument is the prototype object and the second is an object with the specific properties that you want your newly created object to inherit. This is a more optimal way of creating objects than using the new keyword because there is no need to invoke or declare a constructor function. It also helps to make your program more dynamic since you will be creating objects that have different prototypes based on the arguments provided.

Object.create()通常用于创建可以显式​​定义的原型。 如果使用new关键字创建新对象,则对对象的属性没有太多控制权。 Object.create()函数接受两个参数:第一个参数是原型对象,第二个参数是具有希望新创建的对象继承的特定属性的对象。 与使用new关键字相比,这是一种创建对象的最佳方法,因为不需要调用或声明构造函数。 这也有助于使程序更具动态性,因为您将根据提供的参数创建具有不同原型的对象。

Prototypes can be a confusing concept to understand in JavaScript programming, but once you start using them you will be able to produce more fast and efficient applications.

在JavaScript编程中,原型可能是一个令人困惑的概念,但是一旦开始使用它们,您将能够生成更快,更高效的应用程序。

翻译自: https://medium.com/dev-genius/what-are-prototypes-in-javascript-5436b7c6d8e0

javascript原型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值