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

常州做网站信息搜索引擎优化

常州做网站信息,搜索引擎优化,连云港网站关键词,宁波seo专员在 ES6 的 class 中,super 关键字的行为在 构造函数 和 非构造函数(普通方法) 中有显著区别,主要体现在以下方面: 1. 构造函数中的 super 行为规则 必须显式调用: 在子类的构造函数中,必须先调…

在 ES6 的 class 中,super 关键字的行为在 构造函数非构造函数(普通方法) 中有显著区别,主要体现在以下方面:


1. 构造函数中的 super

行为规则
  1. 必须显式调用
    在子类的构造函数中,必须先调用 super(),才能使用 this。否则会抛出错误:

    class Child extends Parent {constructor() {this.name = "Alice"; // ❌ 错误:Must call super constructor in derived classsuper();}
    }
    
  2. super 作为函数调用
    super() 的作用是调用父类的构造函数,并初始化子类实例的 this

    • 参数需传递给父类构造函数。
    • 相当于 Parent.prototype.constructor.call(this, ...args)
  3. 调用顺序
    super() 必须位于构造函数的最顶部(在访问 this 之前)。

示例
class Parent {constructor(name) {this.name = name;}
}class Child extends Parent {constructor(name, age) {super(name); // ✅ 必须先调用 super()this.age = age;}
}const child = new Child("Alice", 10);
console.log(child.name); // "Alice"
console.log(child.age);  // 10

2. 非构造函数中的 super(普通方法)

行为规则
  1. super 作为对象使用
    在普通方法中,super 指向 父类原型(Parent.prototype),用于调用父类方法。

    • super.method() 等价于 Parent.prototype.method.call(this)
  2. 可调用父类原型方法
    通过 super 可以访问父类原型上定义的方法,但无法直接访问父类实例属性。

  3. 无需强制调用顺序
    可以在方法内任意位置使用 super,无需在访问 this 之前调用。

示例
class Parent {sayHello() {console.log("Hello from Parent");}
}class Child extends Parent {sayHello() {super.sayHello(); // ✅ 调用父类方法console.log("Hello from Child");}
}const child = new Child();
child.sayHello(); 
// 输出:
// "Hello from Parent"
// "Hello from Child"

关键区别总结

特性构造函数中的 super非构造函数中的 super
调用形式必须作为函数调用(super()作为对象调用(super.method()
必要性必须显式调用,否则报错可选调用(根据需要)
作用初始化 this,继承父类实例属性调用父类原型方法
访问 this 的时机必须在 super() 调用之后可直接使用 this(无需前置条件)
静态方法中的行为无(静态方法无构造函数)super 指向父类(而非父类原型)

静态方法中的 super

在静态方法中,super 指向 父类本身,而非父类原型:

class Parent {static staticMethod() {console.log("Parent static method");}
}class Child extends Parent {static staticMethod() {super.staticMethod(); // 调用父类的静态方法console.log("Child static method");}
}Child.staticMethod(); 
// 输出:
// "Parent static method"
// "Child static method"

常见错误场景

1. 构造函数中遗漏 super
class Child extends Parent {constructor() { // ❌ 未调用 super(),抛出错误}
}
2. 在非构造函数中误用 super()
class Child extends Parent {method() {super(); // ❌ 语法错误:'super' keyword unexpected here}
}
3. 错误访问父类实例属性
class Parent {constructor() {this.value = 42;}
}class Child extends Parent {getValue() {return super.value; // ❌ 无法通过 super 访问父类实例属性(返回 undefined)}
}const child = new Child();
console.log(child.getValue()); // undefined

总结

  • 构造函数中的 super
    强制调用,用于初始化 this,确保父类实例属性正确继承。
  • 非构造函数中的 super
    用于调用父类原型方法,无需强制顺序,但需注意 super 的指向。
  • 静态方法中的 super
    指向父类本身,用于调用父类静态方法。

文章转载自:

http://RT6O7PYa.hkysq.cn
http://WAYiwI0a.hkysq.cn
http://MRqchmZS.hkysq.cn
http://HW5gMmmI.hkysq.cn
http://XflnD6lu.hkysq.cn
http://euWyDTgj.hkysq.cn
http://0SOeVbHL.hkysq.cn
http://FK3khbID.hkysq.cn
http://HvzW95MC.hkysq.cn
http://Eu6sHX4Y.hkysq.cn
http://UX8nGHPK.hkysq.cn
http://1PbMoIbt.hkysq.cn
http://63iAagn7.hkysq.cn
http://f5dNKCgL.hkysq.cn
http://R6adrpJU.hkysq.cn
http://ZSSeYW03.hkysq.cn
http://Cn9eIhM0.hkysq.cn
http://5ePlIPr4.hkysq.cn
http://anzvMyK2.hkysq.cn
http://jUQOsb5x.hkysq.cn
http://i6PryBiG.hkysq.cn
http://8WuQrV59.hkysq.cn
http://L3ry405i.hkysq.cn
http://5cnFGAHN.hkysq.cn
http://6QemuUBH.hkysq.cn
http://wUM75VIL.hkysq.cn
http://cHaqtntk.hkysq.cn
http://Of91Ot0k.hkysq.cn
http://1IFTIWtI.hkysq.cn
http://rVZVLdHt.hkysq.cn
http://www.dtcms.com/wzjs/750910.html

相关文章:

  • 网站设计团队发展h5游戏盒子
  • 云狄网站建设wordpress手机上发文章
  • 广州网站建设改版做mv主题网站
  • 网站建设网站建设的网络公司安居网站建设
  • php做商城网站怎么做好wordpress 电影模版
  • 淮南市城乡建设档案馆网站怀化建设企业网站
  • 唐卡装饰集团 一站式超级体验店成都个人建网站
  • 网站的seo优化报告西安哪有建网站的
  • 教育类网站框架wordpress主题wind
  • 网站的宣传与推广网站建设详细需求文档
  • igem网站建设乐云seo商城网站建设
  • 企业网站建设 南通网站自助搭建平台
  • 泰州公司网站建设亲情网络广告推广怎么做
  • 河北省网络科技网站商务网站建设调研
  • 禄丰网站建设鼓楼做网站价格
  • 龙岗网站设计案例免费咨询中心
  • 信息设计网站东莞网站seo公司
  • 做网站应该怎么做微信上登录网站同步怎么做
  • 东莞网站建设公司做微商网站设计
  • 深圳南山企业网站建设wordpress添加自定义字段面板
  • 生鲜网站开发背景在线视频播放网站开发
  • vps上创建网站龙岗网站建设开发设计公司
  • 浙江做网站公司oracle数据库做的网站
  • 苏州建网站的公司怎样做网站上的语种链接
  • 高安网站建设公司中国建设人才网信息网证书是假的吗
  • 智联招聘网站建设情况网站开发大致需要哪些步骤
  • wordpress 很慢苏州百度seo代理
  • zencart网站药品行业做网站
  • 申请自己的网站网站开发 手机 电脑
  • 网站图标下载中文html5网站欣赏