- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在学习javascript并感到困惑。 here 上的示例其中有如下示例: -
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
alert ('I am walking!');
};
Person.prototype.sayHello = function(){
alert ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);// <---- Confusion
}
// inherit Person
Student.prototype = new Person(); //<---- Confusion
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
alert('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
alert('goodBye');
}
var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();
// check inheritance
alert(student1 instanceof Person); // true
alert(student1 instanceof Student); // true
<----
)。当我说
Person.call(this);
,这只是说明继承 Person 类的属性......对吗?
// inherit Person
Student.prototype = new Person(); //<---- Confusion
.prototype
还继承所有属性?
最佳答案
为了解释它,首先让我们记住构造函数在 JavaScript 中是如何工作的:
function Guide(a) {
this.a = a;
}
Guide.prototype.q = "Life, the Universe, and Everything";
var g = new Guide(42);
console.log(g.q); // "Life, the Universe, and Everything"
console.log(g.a); // 42
new Guide(42)
,
new
运算符创建一个新对象并使用
Guide.prototype
为其分配一个原型(prototype)。属性(property)。然后
new
来电
Guide
,将该新对象作为
this
传入.
Guide
使用
this
向新对象添加不在其原型(prototype)上的属性。然后
new
表达式完成,其结果是它创建的新对象。
g.q
, 因为
g
对象没有自己的名为
q
的属性,JavaScript 引擎查看
g
的原型(prototype),它(再次)在创建时被分配。该原型(prototype)具有
q
属性,因此引擎使用它的值。
g.a
,
g
对象有自己的属性,称为
a
,因此直接使用该值。
Student
和
Parent
:
function Student() {
// Call the parent constructor
Person.call(this);// <---- Confusion
}
new Student()
,在对
Student
的调用中,
this
是(再次)由
new
创建的新对象运算符,具有
Student.prototype
作为其底层原型(prototype)。但是
Parent
函数没有机会对这个新对象做任何事情。所以那条线的作用是给
Parent
有机会对通过原型(prototype)无法做到的新对象做任何需要做的事情,比如我们的
Guide
之前分配给
this.a
的函数.在技术方面,
Parent.call(this);
调用
Parent
功能,确保
this
调用
Parent
是传递给
call
的值(在这种情况下,就是对
this
的调用的
Student
——例如,新对象)。如果你熟悉基于类的语言,这就像做
super();
(那是 Java,但你明白了)在派生构造函数中:它为基构造函数提供了初始化对象的机会。
// inherit Person
Student.prototype = new Person(); //<---- Confusion
Student
应该继承自
Person
,该代码所做的是创建原型(prototype),该原型(prototype)将分配给通过
new Student
创建的对象.它创建的对象是
Person
目的。
Person
比它应该更频繁[创建
Student.prototype
和 时调用
Student
时],并且未能设置
constructor
)。
Student
的
prototype
属性,如果我们要调用
Parent
来自
Student
,看起来像这样:
function derive(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor();
}
derive(Student, Parent);
Parent.prototype
的原型(prototype)。但没有调用
Parent
.这是一个非此即彼的选择:要么调用
Parent
创建
Student.prototype
, 或调用
Parent
在
Student
内,但不要两者都做。使用构造函数构建层次结构时,通常希望从子构造函数调用父级,而不是在创建子原型(prototype)时。当使用直接对象继承(没有构造函数)时,当然你用另一种方式。
Lineage
的帮助脚本。你可能想看看,特别是即使你不使用
Lineage
,
this discussion在其 wiki 页面上可能有助于理解继承层次结构。
关于Javascript原型(prototype)和调用之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631180/
我需要修复 getLineNumberFor 方法,以便如果 lastName 的第一个字符位于 A 和 M 之间,则返回 1;如果它位于 N 和 Z 之间,则返回 2。 在我看来听起来很简单,但我不
您好,感谢您的帮助!我有这个: 0 我必须在每次点击后增加“pinli
Javascript 中是否有一种方法可以在不使用 if 语句的情况下通过 switch case 结构将一个整数与另一个整数进行比较? 例如。 switch(integer) { case
我有一列是“日期”类型的。如何在自定义选项中使用“之间”选项? 最佳答案 请注意,您有2个盒子。 between(在SQL中)包含所有内容,因此将框1设置为:DATE >= startdate,将框2
我有一个表,其中包含年、月和一些数字列 Year Month Total 2011 10 100 2011 11 150 2011 12 100 20
这个问题已经有答案了: Extract a substring between double quotes with regular expression in Java (2 个回答) how to
我有一个带有类别的边栏。正如你在这里看到的:http://kees.een-site-bouwen.nl/ url 中类别的 ID。带有 uri 段(3)当您单击其中一个类别时,例如网页设计。显示了一
这个问题在这里已经有了答案: My regex is matching too much. How do I make it stop? [duplicate] (5 个答案) 关闭 4 年前。 我
我很不会写正则表达式。 我正在尝试获取括号“()”之间的值。像下面这样的东西...... $a = "POLYGON((1 1,2 2,3 3,1 1))"; preg_match_all("/\((
我必须添加一个叠加层 (ImageView),以便它稍微移动到包含布局的左边界的左侧。 执行此操作的最佳方法是什么? 尝试了一些简单的方法,比如将 ImageView 放在布局中并使用负边距 andr
Rx 中是否有一些扩展方法来完成下面的场景? 我有一个开始泵送的值(绿色圆圈)和其他停止泵送的值(簧片圆圈),蓝色圆圈应该是预期值,我不希望这个命令被取消并重新创建(即“TakeUntil”和“Ski
我有一个看起来像这样的数据框(Dataframe X): id number found 1 5225 NA 2 2222 NA 3 3121 NA 我有另一个看起来
所以,我正在尝试制作正则表达式,它将解析存储在对象中的所有全局函数声明,例如,像这样 const a = () => {} 我做了这样的事情: /(?:const|let|var)\s*([A-z0-
我正在尝试从 Intellivision 重新创建 Astro-Smash,我想让桶保持在两个 Angular 之间。我只是想不出在哪里以及如何让这个东西停留在两者之间。 我已经以各种方式交换了函数,
到处检查但找不到答案。 我有这个页面,我使用 INNER JOIN 将两个表连接在一起,获取它们的值并显示它们。我有这个表格,用来获取变量(例如开始日期、结束日期和卡号),这些变量将作为从表中调用值的
我陷入了两个不同的问题/错误之间,无法想出一个合适的解决方案。任何帮助将不胜感激 上下文、FFI 和调用大量 C 函数,并将 C 类型包装在 rust 结构中。 第一个问题是ICE: this pat
我在 MySQL 中有一个用户列表,在订阅时,时间戳是使用 CURRENT_TIMESTAMP 在数据库中设置的。 现在我想从此表中选择订阅日期介于第 X 天和第 Y 天之间的表我尝试了几个查询,但不
我的输入是开始日期和结束日期。我想检查它是在 12 月 1 日到 3 月 31 日之间。(年份可以更改,并且只有在此期间内或之外的日期)。 到目前为止,我还没有找到任何关于 Joda-time 的解决
我正在努力了解线程与 CPU 使用率的关系。有很多关于线程与多处理的讨论(一个很好的概述是 this answer )所以我决定通过在运行 Windows 10、Python 3.4 的 8 CPU
我正在尝试编写 PHP 代码来循环遍历数组以创建 HTML 表格。我一直在尝试做类似的事情: fetchAll(PDO::FETCH_ASSOC); ?>
我是一名优秀的程序员,十分优秀!