首页 \ 问答 \ 如果我们创建子类的对象,是否会创建父类的对象?(will there be object creation of parent class if we create an object of child class?)

如果我们创建子类的对象,是否会创建父类的对象?(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
更新时间:2022-04-10 21:04

最满意答案

正如已经提到的那样, 构成与继承原则是一样的。 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 ...
  • 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 ...
  • 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类实际上只是构造函数和原型初始化的语法糖。 也就是说, 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 ...
  • 不,至少还没有。 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是完全没问题 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)