Javascript 在 Child 中调用 Parent 构造函数(原型继承) - 它是如何工作的?

2023-11-27

我知道它有效,但我不知道为什么以及如何。有哪些机制?

// Parent constructor
function Parent(name){
  this.name = name || "The name property is empty";
}

// Child constructor
function Child(name){
  this.name = name;
}

// Originaly, the Child "inherit" everything from the Parent, also the name property, but in this case
// I shadowing that with the name property in the Child constructor.
Child.prototype = new Parent();

// I want to this: if I dont set a name, please inherit "The name property is empty" from the 
// Parent constructor. But I know, it doesn't work because I shadow it in the Child.
var child1 = new Child("Laura");
var child2 = new Child();

//And the result is undefined (of course) 
console.log(child1.name, child2.name);  //"Laura", undefined

我知道我需要什么,call() or the apply()方法。致电“超一流" (the Parent构造函数)来自Child,并通过this对象和论点name对此。有用:

function Parent(name){
  this.name = name || "The name property is empty";
}

function Child(name){
  // Call the "super class" but WHAT AM I DO? How does it work? I don't understand the process, I lost the line.
  Parent.call(this, name);
}

Child.prototype = new Parent();

var child1 = new Child("Laura");
var child2 = new Child();

console.log(child1.name, child2.name); // "Laura", "The name property is empty"

它工作完美,但我不明白会发生什么。我失去了this在我的脑海里,我无法遵循这个过程call()方法。是否从复制构造函数体Parent to the Child或者是什么?哪里是this目的?为什么它有效?

请帮忙描述一下过程,我不太明白。


首先,停止做Child.prototype = new Parent();用于继承,除非您的浏览器不支持任何其他替代方案。这是一种非常糟糕的风格,并且可能会产生不良的副作用,因为它实际上运行构造函数逻辑。

您可以使用Object.create现在在每个现代浏览器中。

Child.prototype = Object.create(Parent.prototype);

请注意,此后您还应该修复constructor的财产Child.prototype以便它正确地指向Child而不是Parent.

Child.prototype.constructor = Child;

接下来,如何call作品?出色地call允许指定哪个对象将被引用this函数执行时的关键字。

function Child(name){
  //When calling new Child(...), 'this' references the newly created 'Child' instance

  //We then apply the 'Parent' constructor logic to 'this', by calling the 'Parent' function
  //using 'call', which allow us to specify the object that 'this' should reference 
  //during the function execution.
  Parent.call(this, name);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript 在 Child 中调用 Parent 构造函数(原型继承) - 它是如何工作的? 的相关文章

随机推荐

  • 如何使用 python 替换/删除 pdf 中的文本? [复制]

    这个问题在这里已经有答案了 我有隐藏 pdf 部分的代码 只需用白色多边形覆盖它 但问题是 文本仍然是there 如果你按 ctrl f 仍然可以找到它 我的目标是实际从 pdf 本身中删除文本 使用 pdfminer 我设法从 pdf 中
  • 为什么 App_Offline 一旦开始加载 dll 就无法工作?

    有人可以帮我解决这个问题吗 在生产站点上 app offline htm 仅在您开始上传 dll 之前有效 一旦您开始上传 dll 它就会抛出以下错误 无法加载文件或程序集 SubSonic 或其依赖项之一 该进程无法访问该文件 因为该文件
  • 关于使用 tf.image.crop_and_resize

    我正在研究适用于 fast rcnn 的 ROI 池化层 并且我习惯使用张量流 我发现tf image crop and resize可以充当 ROI 池化层 但我尝试了很多次都无法得到我期望的结果 或者真正的结果正是我得到的吗 这是我的代
  • sprintf 何时以及为何会失败?

    我正在使用 swprintf 将字符串构建到缓冲区中 使用循环等 const int MaxStringLengthPerCharacter 10 1 wchar t pTmp pBuffer for size t i 0 i lt nNu
  • Java 中的 HTTP URL 地址编码

    我的 Java 独立应用程序从用户那里获取一个 URL 指向一个文件 我需要点击它并下载它 我面临的问题是我无法正确编码 HTTP URL 地址 例子 URL http search barnesandnoble com booksearc
  • 在 JavaFx 中从输入流播放 mp3 文件

    我正在使用 JavaFX 媒体播放器使用以下代码播放 mp3 文件 new MediaPlayer new Media FileObject toURI toString play 但是现在我需要在内存中保存 mp3 字节数据而不是文件对象
  • 强制浏览器使用新的 CSS

    有没有办法检查用户的浏览器是否缓存了不同版本的 CSS 如果是 则强制他们的浏览器提取新版本 我不知道这是否是正确的用法 但我认为您可以使用查询字符串强制重新加载 css 文件 我记得几年前我使用过这种方法来强制重新加载网络摄像头图像 但时
  • 编译器之间的 Dll 兼容性

    有没有办法让不同编译器构建的 c dll 相互兼容 这些类可以具有用于创建和销毁的工厂方法 因此每个编译器都可以使用自己的 new delete 因为不同的运行时有自己的堆 我尝试了以下代码 但它在第一个成员方法上崩溃了 接口 h prag
  • Composer 给出错误,“未找到类”

    我使用的是 Windows 10 创建文件夹后src在根目录中我创建了两个文件 目录结构 运行前composer install composer json run php src childclass php parentclass ph
  • 我怎样才能过渡高度:0;高度:自动;使用CSS?

    我正在尝试做一个 ul 使用 CSS 过渡向下滑动 The ul 开始于height 0 悬停时 高度设置为height auto 然而 这导致它简单地出现 not过渡 如果我从height 40px to height auto 然后它会
  • jQuery:强制显示修改后的dom

    我在尝试在页面上设置一个 加载微调器 时遇到了一个问题 该微调器在对表进行排序时运行 特别是对于速度较慢的客户端 因为对页面进行排序可能需要长达 10 秒的时间 我可以看到 DOM 被微调器代码修改 但它不显示 我希望在排序发生之前我可以做
  • C# 将对象写入二进制文件

    我必须将一个对象写入二进制文件 我的结构如下所示 Struct Company int numberofemployees list of Struct Employee Struct Employee string EmployeeNam
  • 通过 Applescript 添加文件到 Xcode 项目目标

    我想将文件自动添加到从头开始创建的 Xcode 项目 通过另一个 IDE 作为构建后步骤 我们的项目设置为调用一个 applescript 该 applescript 在项目中进行适当的文件引用 但每次尝试将文件引用添加到项目都会失败 核心
  • Scala:删除对象列表中的重复项

    我有一个对象列表List Object 它们都是从同一个类实例化的 这个类有一个必须是唯一的字段Object property 迭代对象列表并删除具有相同属性的所有对象 但第一个 的最干净的方法是什么 list groupBy proper
  • 页面刷新后 Laravel 会话丢失

    我对 Laravel 中的会话有一个小问题 我做了这样的身份验证功能 public function postSignin attempt Auth attempt array username gt Input get username
  • Clang 不会编译 gcc 会编译的模板专业化

    Gcc 可以正常编译 但 Clang trunk 拒绝并显示以下消息
  • 在 Android 4.4.2 上以编程方式切换移动数据

    我一直使用此代码以编程方式启用移动数据 ConnectivityManager conman ConnectivityManager context getApplicationContext getSystemService Contex
  • Docker 将 PHP 容器连接到 MySQL

    我有两个容器 一个 apache php 容器和一个 mysql db 容器 我正在尝试让我的 php 脚本来查询我的 sql 数据库 然而 我收到以下错误 Fatal error Uncaught PDOException PDO con
  • Windows 命名管道实践

    对于Windows命名管道 使用命名管道的正确方法是什么CreateNamedPipe ConnectNamedPipe DisconnectNamedPipe and CloseHandle calls 我正在制作一个服务器应用程序 它连
  • Javascript 在 Child 中调用 Parent 构造函数(原型继承) - 它是如何工作的?

    我知道它有效 但我不知道为什么以及如何 有哪些机制 Parent constructor function Parent name this name name The name property is empty Child constr