javascript - Javascript中的实例是什么意思
问题描述
我很难理解实例属性的含义
例如在 Firefox ES6 Class 文档中它说 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
实例属性
实例属性必须在类方法中定义
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
我只是将其视为具有构造函数的类,那么实例属性是什么意思?
解决方案
实例属性有height
和width
:
class Rectangle {
constructor(height, width) {
this.height = height;
// -----^^^^^^
this.width = width;
// -----^^^^^
}
}
“实例”是一个对象。人们倾向于将“实例”与使用类语法的代码相关联,但它仅表示对象(尽管通常暗示是“特定类的对象”)。
与“静态”或“类”属性对比:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Rectangle.FOUR_BY_FOUR = new Rectangle(4,4);
那里有FOUR_BY_FOUR
一个“类/静态”属性。或者使用静态类特性提案(目前是第 3 阶段)提出的语法:
class Rectangle {
static FOUR_BY_FOUR = new Rectangle(4,4);
constructor(height, width) {
this.height = height;
this.width = width;
}
}
推荐阅读
- java - Android Instrumentation 测试:使用 Espresso 和 Spoon 的“找不到测试类”
- php - WordPress 命令不同步
- powerbi - 如何在 Power BI 中创建具有互补日期范围的两列的累积和
- flutter - 在 Flutter/dart 中使用泛型类型解析 JSON 的最佳方法
- asp.net-core - 如何添加声明以在身份服务器中使用它们
- bootstrap-datetimepicker - Datetimepicker 不显示日历
- python - 我是使用 python 制作不和谐欢迎机器人的新手,我遇到了问题
- terraform - 如何使用 terraform 0.13.5 检查列表中是否存在值?
- python - 在 Windows 上识别 Python 中丢失的 DLL
- git - 将 1 个分支合并到另一个分支,但不是所有文件/文件夹