当前位置: 首页 > news >正文

注册网站地址秦皇岛网站设计

注册网站地址,秦皇岛网站设计,wordpress新增文章小工具,建设网站功能定位在现代编程语言中#xff0c;类是面向对象编程范式中的核心概念之一。 与函数类似#xff0c;类本质上是一种特殊的函数#xff0c;它允许我们将数据和操作封装在一起#xff0c;以创建具有共同行为和状态的对象。 在类的世界里#xff0c;我们有类表达式和类声明#xf… 在现代编程语言中类是面向对象编程范式中的核心概念之一。 与函数类似类本质上是一种特殊的函数它允许我们将数据和操作封装在一起以创建具有共同行为和状态的对象。 在类的世界里我们有类表达式和类声明它们各自具有自己的特性和用途。 类 ✨ 类本质上是一种特殊的函数。所以和函数一样有类表达式和类声明 类声明 与函数不同类声明不会被提升。这意味着在使用类之前需要先进行类声明。类声明通常包括构造函数和其他成员方法。构造函数是一个特殊的方法用于创建和初始化类所创建的对象。 // 类声明 class Rectangle {constructor(height, width) {this.height height; // 实例成员this.width width;} }let p new Rectangle();类表达式 类表达式可以命名也可以不命名我们可以通过类的name属性来检索 // 未命名/匿名类 let Rectangle class {constructor(height, width) {this.height height;this.width width;} }; console.log(Rectangle.name); // output: Rectangle// 命名类 // 命名类表达式的名称是该类体的局部名称。 let Rectangle class Rectangle2 {constructor(height, width) {this.height height;this.width width;} }; console.log(Rectangle.name); // 输出Rectangle2类的定义 {}中的部分叫做类体。类体中会包括 构造函数 constructor方法是一个特殊的方法这种方法用于创建和初始化一个由class创建的对象。注意⚠️一个类体中只能有一个constructor方法使用 super 关键字来调用一个父类的构造函数 原型方法 class Rectangle {// constructorconstructor(height, width) { // 实例的属性必须定义在类的方法里this.height height;this.width width;}// Getterget area() {return this.calcArea();}// MethodcalcArea() {return this.height * this.width;} } const square new Rectangle(10, 10);console.log(square.area); // 100静态方法 static来定义静态方法只能被类访问 class Point {constructor(x, y) {this.x x;this.y y;}static displayName Point;static distance(a, b) {const dx a.x - b.x;const dy a.y - b.y;return Math.hypot(dx, dy);} }const p1 new Point(5, 5); const p2 new Point(10, 10); p1.displayName; // undefined p1.distance; // undefinedconsole.log(Point.displayName); // Point console.log(Point.distance(p1, p2)); // 7.0710678118654755getter和setter 类体中遵循严格模式 this指向 类中 类体中的成员方法遵循严格模式。this在类方法中的行为与传统函数有所不同。在调用静态或原型方法时this默认指向undefined但在非严格模式下会自动装箱以保留传入状态。 当被调用时。谁调用指向谁 class Animal { // 原型方法speak() {return this;} // 静态方法static eat() {return this;} }let obj new Animal(); obj.speak(); // Animal {} let speak obj.speak; speak(); // undefinedAnimal.eat(); // class Animal let eat Animal.eat; eat(); // undefined传统函数中 在非严格模式下调用函数会发生自动装箱。即如果初始值时undefined则this指向全局对象。 function Animal() {}Animal.prototype.speak function () {return this; };Animal.eat function () {return this; };let obj new Animal(); let speak obj.speak; speak(); // global objectlet eat Animal.eat; eat(); // global object字段声明 公有字段 不需要let, const等关键字预先声明 class Rectangle {height 0;width;constructor(height, width) {this.height height;this.width width;} }私有字段 只能在类内部读取外部无法调用。私有字段仅能在字段声明中预先定义。 class Rectangle {#height 0;#width;constructor(height, width) {this.#height height;this.#width width;} }extends 我们可以创建一个子类来扩展父类的功能。子类继承了父类的属性和方法并可以在其基础上进行扩展或重写。 class Father {constructor(name) {this.name name;}speak() {console.log(${this.name} makes a noise.);} }class Son extends Father {constructor(name) {super(name); // 调用超类构造函数并传入 name 参数}speak() {console.log(${this.name} barks.);} }var d new Son(Mitzie); d.speak(); // Mitzie barks.super super 关键字用于调用对象的父对象上的函数. class Father {constructor(name) {this.name name;}speak() {console.log(this.name makes a noise.);} }class Son extends Father {speak() {super.speak();console.log(this.name roars.);} }| 本文参考MDN
http://www.hkea.cn/news/14540401/

相关文章:

  • 洛阳网站建设找洛阳铭信网络新余服装网站建设
  • 注册分公司流程及所需资料seo是什么味
  • 浙江 外贸网站建设买完域名接下来怎么弄
  • 成都网站建设哪家好山东省住房和城乡城乡建设厅网站
  • 郑州网站建设哪家强广州市研发网站建设价格
  • 柬埔寨网站建设开发区网站建设在哪
  • 游戏网站建设的目的电子产品定制开发
  • 电商erp网站开发公共资源交易中心级别
  • 为什么建设网站要年年交钱常见的网络营销形式有
  • 企业网站自助建设现在还有做网站的必要吗
  • 中企动力做的网站山西太原销售网站模板
  • 网站备案后可以修改吗化妆品网站建设需求问卷调查
  • 西安市建设工程信息网诚信平台官网长沙做网站seo优化外包
  • 网站建设的主要内容包括建设京东物流网站的目标是什么
  • 无锡八匹马网站建设腾讯邮箱企业邮箱登录
  • 网站标准字体样站内seo是什么意思
  • 正规营销型网站建设如何用两台电脑做服务器建网站
  • 长沙建站公司动画制作app
  • 网站开发网络公司flash 网站头部
  • 免费做网站的网站长沙建站公司模板
  • 做网站要备案吗 要几天网站设计济南
  • 用jsp做的网站代码上海做网站最低价
  • 山西省大同市网站建设公司重庆做网站开发的公司有哪些
  • 网站里添加图片超链接怎么做软件设计工资一般多少
  • 做企业网站申请域名百度指数查询工具
  • 爬虫做网站相关教程wordpress文章付费阅读设置
  • 广安们内网站建设Lms wordpress功能
  • 中山服装网站建设东莞阳光官方网
  • 关于网站建设的职位北京怎样做企业网站
  • 台州网站公司网站建设使页面内容居中