javascript原型_了解javascript原型和原型链接

news/2024/7/19 14:41:35 标签: javascript, js

javascript原型

Image for post
JavaScript
JavaScript

JavaScript原型 (JavaScript Prototype)

Prototypes are the mechanism by which JavaScript objects inherit features from one another. Prototypes can be used to add properties and methods into existing constructor.

原型是一种机制,JavaScript对象通过该机制彼此继承特征。 原型可用于将属性和方法添加到现有构造函数中。

Lets say, We have one constructor function:

可以说,我们有一个构造函数:

function Person(name) {this.name = name;}

Now, I am creating a instance to access members of this constructor

现在,我正在创建一个实例来访问此构造函数的成员

let obj = new Person(‘Premal’);

Now, you can access properties like this:

现在,您可以访问如下属性:

console.log(‘Name ’, obj.name);

Till this example everything was okay but what if I want to add one property into an existing constructor?

直到这个示例一切正常,但是如果我想向现有构造函数添加一个属性怎么办?

You can’t add it directly like this.

您不能像这样直接添加它。

Person.gender = ‘Male’;

It will give you undefined when you try to access it.

当您尝试访问它时,它将给您未定义的信息。

Instead, use a prototype to add properties and methods in the constructor.

而是使用原型在构造函数中添加属性和方法。

Person.prototype.gender = 'male'

JavaScript原型链 (JavaScript Prototype Chaining)

type Object.prototype in console and see what happens

在控制台中输入Object.prototype,看看会发生什么

Image for post
JavaScript prototype
JavaScript原型

You can see in the above image that a large number of methods are defined in Object’s prototype property. This is called prototype chaining. These properties will be available in objects that inherit from Object.

您可以在上图中看到在Object的prototype属性中定义了许多方法。 这称为原型链。 这些属性在从Object继承的对象中可用。

Let’s learn from example:

让我们从示例中学习:

let obj = {};

I have defined one object above that inherits from Object.

我在上面定义了一个继承自Object的对象。

Now, try to call this variable named obj

现在,尝试调用名为obj的变量

Write obj. In console

写obj。 在控制台中

Image for post
JavaScript Prototype Chaining
JavaScript原型链

Code will show all the predefined methods of Object that we have seen earlier.

代码将显示我们之前看到的Object的所有预定义方法。

As we know all JavaScript objects inherit properties and methods from prototype. We can see another example of prototype chaining.

众所周知,所有JavaScript对象都从原型继承属性和方法。 我们可以看到原型链接的另一个示例。

lets define one string variable in console:

让我们在控制台中定义一个字符串变量:

let a = ‘premal’

type a. and see all the methods and properties of string called JavaScript prototype chaining

输入a。 并查看称为JavaScript原型链的字符串的所有方法和属性

Image for post
JavaScript Prototype Chaining
JavaScript原型链

You can see all string methods and properties that is what prototype chaining is.

您可以看到原型链接就是所有的字符串方法和属性。

Source of Blog: http://www.premalkatigar.com/technology/understanding-javascript-prototype-and-prototype-chaining/

博客来源: http : //www.premalkatigar.com/technology/understanding-javascript-prototype-and-prototype-chaining/

Happy coding!

编码愉快!

翻译自: https://medium.com/@premal.katigar/understanding-javascript-prototype-and-prototype-chaining-1378cbe32aa7

javascript原型


http://www.niftyadmin.cn/n/1468612.html

相关文章

flutter怎么添加ios网络权限_究竟是谁走漏了风声?我已经用 Flutter了

点击上方“开发者技术前线”,选择“星标”12:00 在看 真爱作者: 刘望舒 | 来源:刘望舒前言最近的Google I/O大会上,Flutter1.5 预览版开始支持移动、Web、桌面和嵌入式设备,从不温不火的sky一直进化到如今热门的Flutt…

C#学习(十三)——C# 使用 GDI+ 画图

GDI - 图形设备接口(Graphics Device Interface) GDI是图形设备接口的英文缩写,主要任务是负责系统与绘图程序之间的信息交换,处理所有Windows程序的图形和图像输出。GDI的出现使程序员无需要关心硬件设备及设备正常驱动,就可以将应用程序的…

雅典娜雅典娜

If you’re using AWS for data transformation, you’re going to run into Athena sooner or later. Athena allows you to query data across multiple data stores, with a well-known SQL syntax (Presto 6.15).如果您使用AWS进行数据转换,则迟早会遇到Athena。…

C#学习(十四)——C# “从元数据”

所谓元数据.其实是指由微软所封装的类…这些类你可以查看.但是无法修改。 元数据是一种二进制信息,用以对存储在公共语言运行库可移植可执行文件 (PE) 文件或存储在内存中的程序进行描述。将您的代码编译为 PE 文件时,便会将元数据插入到该文件的一部分…

apache httpclient 连接池 工具_最近学习了Http连接池

起因6.1大促值班发现的一个问题,一个rpc接口在0~2点用户下单高峰的时候表现rt高(超过1s,实际上针对性优化过的接口rt超过这个值也是有问题的,通常rpc接口里面即使逻辑复杂,300ms应该也搞定了),可以理解,但是…

C#学习(十五)——窗体控件用法大全

C#控件及常用设计整理 1、窗体 1.1、常用属性** (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体。 (2) WindowState属性: 用来获取或设置窗体的窗口状态。 取值…

lyft读音_把lyft翻译成西班牙语

lyft读音This article is a translation. Original publication here.本文是翻译。 原始出版物在这里 。 In the United States, the Latinx community comprises a large part of the population. Therefore, many of Lyft’s drivers and riders speak Spanish. However, be…

python接口自动化参数化_Python接口自动化(七) 参数化关联接口

我们用自动化新建项目之后,要想接着对这个新建项目操作,那就需要用参数关联了,新建项目之后会有一个项目的id,获得到这个id,就可以通过这个项目id继续操作这个新建项目。 大致流程步骤:登录——>新建项目…