gpt4 book ai didi

javascript - var 原型(prototype) = new this();有人可以解释 John resig 在他的类继承博客中使用的这个语句吗?

转载 作者:行者123 更新时间:2023-11-28 20:26:39 25 4
gpt4 key购买 nike

博客网址 -> http://ejohn.org/blog/simple-javascript-inheritance/

这是片段。

var _super = this.prototype;
initializing = true;
var prototype = new this();
initializing = false;

我对 new this(); 的使用感到困惑

最佳答案

在上下文中查看它会有所帮助。即:

Class.extend = function(prop) {
var _super = this.prototype;

// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;

请注意,extendClass 的方法(即函数属性)。在任何方法中,this 引用它们是 [1] 方法的对象。所以在Class.extend里面,this === Class。因此 new this() 等价于 new Class()

他这样做的原因有点奇怪。他试图建立某种“类层次结构”,其中所有内容都源自 Class,有点像 Java 或 C# 中的所有内容都源自 Object

我不推荐这种方法。

<小时/>

[1] 仅当该方法作为方法调用时才成立,例如Class.extend(...),而不是作为函数调用时,例如var 扩展 = Class.extend;扩展(...)

关于javascript - var 原型(prototype) = new this();有人可以解释 John resig 在他的类继承博客中使用的这个语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17263777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com