如果我们创建子类的对象,是否会创建父类的对象?(will there be object creation of parent class if we create an object of child class?)
我知道每次创建子类对象时都会调用super。 但是,我特别想知道的是它会加载类还是会创建该父类的对象。
提前致谢。
I know that super will be called every time we create a child class object. But, what I specifically want to know is that will it load the class or will it create the object of that parent class.
Thanks in advance.
原文:https://stackoverflow.com/questions/49627652
最满意答案
正如已经提到的那样, 构成与继承原则是一样的。 Worker对象应该将
ipc
实例保留为私有属性,并在它应该公开其中的某些方法时将它的方法进行包装:class Worker { constructor(ipc) { this.ipc = ipc; } send(...args) { return this.ipc.send(...args); } }
As it was already mentioned, it is the case for composition over inheritance principle. Worker object is supposed to keep
ipc
instance as private property and wrap its methods if it is supposed to expose some of them:class Worker { constructor(ipc) { this.ipc = ipc; } send(...args) { return this.ipc.send(...args); } }
相关问答
更多-
箭头功能提供了一个词汇。 它使用在评估函数时可用的this一点。 它在逻辑上相当于(以下不是有效的代码,因为你不能有一个名为this的变量): (function(this){ // code that uses "this" })(this) 在你的第一个例子中,箭头函数在构造函数中,并且指向新生成的实例。 在你的第三个例子中,没有使用箭头函数,标准的this行为一如既往(这在函数范围中)。 在你的第二个例子中,你使用了一个箭头函数,但是在它被评估的范围内, this是全局/未定义的。 Arro ...
-
ES6类变量替代(ES6 class variable alternatives)[2023-09-29]
ES维基中关于ES6中提案( 最小最小类 )的注释: 有(有意)没有直接的声明方式来定义原型数据属性(除方法之外)类属性或实例属性 需要在声明之外创建类属性和原型数据属性。 在类定义中指定的属性被分配与在对象文字中出现的属性相同的属性。 这意味着你所要求的是被考虑的,并明确地决定了。 但为什么? 好问题。 TC39的好人希望类声明来声明和定义一个类的功能。 不是其成员。 ES6类声明定义其用户的合同。 记住,类定义定义原型方法 - 在原型上定义变量通常不是你所做的。 当然可以使用: constructor( ... -
它是 class Baz { constructor(biz) { Object.getPrototypeOf(this).biz = biz // or // this.constructor.prototype.biz = biz } } const baz1 = new Baz(1); // baz1.biz === 1 const baz2 = new Baz(2); // baz2.biz === 2 baz1.biz === baz2.biz; // === 2 ...
-
避免在es6中导出单例(Avoid exporting singleton in es6)[2023-06-25]
ES模块在首次导入时仅评估一次,这有效地使模块导出单例。 export const getObject = () => ({ user: 'asd', asd: 'b' }) 是工厂函数,在这种情况下它是完全有效的,它更适用于没有继承且没有方法的仅数据对象。 类是一个合适的替代方法(比工厂函数效率稍低,但如果有方法则更好的选择): export class A { constructor() { this.user = 'asd'; this.asd = 'b'; } } 由于 ... -
ES6本身没有您想要的解决方案,但您可以使用ES7类属性: class foo { constructor() { document.body.addEventListener('click', this.clickHandler, false ); } clickHandler = event => { // do stuff document.body.removeEventListener('click', this.click ...
-
ES6类继承的性能(Performance of ES6 class inheritance)[2023-05-26]
ES6类实际上只是构造函数和原型初始化的语法糖。 也就是说, MyClass定义的两个版本都非常相同,并且在所有实现中很可能具有相同的性能特征(不是有一个)。 ES6 classes are really just syntactic sugar for constructor functions and prototype initialisations. That is, both versions of your MyClass definition are pretty much equivalen ... -
II6在ES6类文字中(IIFE in ES6 class literal)[2022-05-15]
不,至少还没有。 ES6类仅支持声明方法,因此任何不直接作为方法的东西(包括间接评估方法的东西,例如IIFE)仍必须使用原型声明。 但是,ES6类实际上与ES5构造函数的工作方式相同,只是语法更清晰,所以你仍然可以这样做: class MyClass { constructor() { /* initialize */ } regularMethod() { /* some stuff */ } } MyClass.prototype.myMethod = (functi ... -
您可能希望将一组方法分组到单个文件中,这些方法不一定总是要一起导入。 例如,实用程序类可能会根据需要列出几个您只想一次导入一个的函数。 导出default ,无论是类封装还是单个函数,都可以清楚地说明该模块的主要用途是什么。 您还可以组合默认导出和命名导出: class User {} function getUserInfo() export default User; export {getUserInfo}; // ... other file ... import User, {getUser ...
-
正如已经提到的那样, 构成与继承原则是一样的。 Worker对象应该将ipc实例保留为私有属性,并在它应该公开其中的某些方法时将它的方法进行包装: class Worker { constructor(ipc) { this.ipc = ipc; } send(...args) { return this.ipc.send(...args); } } As it was already mentioned, it is the case for composition o ...
-
在es6类中,我们也有原型,但它们被类语法掩盖,你应该使用extends关键字向类添加方法。 我不确定你的意思是“蒙面”。 是的,它的语法不同,但结果完全相同 - class创建了一个带有.prototype属性的构造函数。 因此,虽然extends语法当然更好写,但您不能以编程方式使用它。 请注意, extends用于子类化,而不是用于现有类的扩展,因此它无论如何都不适合您的需要。 我不确定如何使用类似于上面的方法以编程方式向ES6类添加方法。 继续使用您已有的方法。 以这种风格做mixins是完全没问题 ...