栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript在Derived.prototype = new Base上使用'new'关键字的原因是什么

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript在Derived.prototype = new Base上使用'new'关键字的原因是什么

WeatherWidget.prototype = new Widget;

new
关键字调用
Widget
构造函数,返回值分配给
prototype
属性。(如果省略
new
Widget
除非添加了参数列表,否则将不会调用
()
。但是,以
Widget
这种方式调用可能是不可能的。如果它不是严格的模式代码并且实现是,则肯定有可能
破坏全局名称空间。 符合ECMAscript版本5.x,因为
this
在构造函数中它将引用ECMAscript的全局对象。)

这样,您的

WeatherWidget
实例将全部从 同一个
Widget
实例继承。原型链将为:

[new WeatherWidget()] → [new Widget()] → [Widget.prototype] → …

这可能很有用,但是大多数时候您都不希望发生这种情况。除非您希望所有

WeatherWidget
实例 在它们之间共享 从该实例(仅 通过
该实例)继承的 属性值
,否则不要在此进行操作。另一个问题是您需要以这种方式调用父构造函数,这可能不允许像您一样在没有参数的情况下调用它,或者无法正确初始化。它与模拟类(例如从Java)继承的继承毫无关系。
Widget

__
Widget.prototype


function Dummy () {}Dummy.prototype = Widget.prototype;WeatherWidget.prototype = new Dummy();WeatherWidget.prototype.constructor = WeatherWidget;

constructor
原型属性应该是固定的为好,这样你的
WeatherWidget
情况下,
w
将有
w.constructor ===WeatherWidget
预期,而不是
w.constructor === Widget
。但是,请注意,此后是可枚举的。

这样一来,

WeatherWidget
情况将通过继承原型链的属性,但不会在它们之间共享的属性值,因为他们继承
Widget.prototype
通过
Dummy
它没有自己的属性:

[new WeatherWidget()] → [new Dummy()] → [Widget.prototype] → …

在ECMAscript Ed的实现中。5及更高版本,您可以并且应该使用

WeatherWidget.prototype = Object.create(Widget.prototype, {  constructor: {value: WeatherWidget}});

代替。这具有额外的优点,即所得的

constructor
属性不可写,不可枚举或不可配置。

仅当从中显式调用父构造函数时,才会调用

WeatherWidget
,例如

function WeatherWidget (…){  Widget.apply(this, arguments);}

另请参见

Function.prototype.extend()
我的JSX:object.js中如何将其概括化。使用该代码,它将变成

WeatherWidget.extend(Widget);

My

Function.prototype.extend()
带有一个可选的第二个参数,您可以使用它轻松地扩展
WeatherWidget
实例的原型:

WeatherWidget.extend(Widget, {  foo: 42,  bar: "baz"});

相当于

WeatherWidget.extend(Widget);WeatherWidget.prototype.foo = 42;WeatherWidget.prototype.bar = "baz";

但是,您仍然需要在子构造函数中显式调用父构造函数。该部分不能合理地自动化。但是我向实例

Function.prototype.extend()
添加了一个
_super
属性,
Function
这使它变得更容易:

function WeatherWidget (…){  WeatherWidget._super.apply(this, arguments);}

其他人也实现了类似的扩展。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号