gpt4 book ai didi

javascript - JavaScript 中的继承

转载 作者:行者123 更新时间:2023-11-29 16:26:06 24 4
gpt4 key购买 nike

我正在将 JavaScript 客户端编码为 REST JSON API。因为我不希望它依赖于任何其他库,所以我使用普通的 javascript 来完成它。

一切都工作得很好,但我在 IE 中的继承方面遇到了麻烦(它适用于所有其他浏览器)。我就是这样做的;

/**
* BaseClass
*/
api.BaseClass = function(something) {
this.someFunction(something);
};

api.BaseClass.prototype.someFunction = function() {
// Code...
};

/**
* Subclass
*/
api.SubClass = function(something) {
// to make the constructor be called in the base
this.base = api.BaseClass;
this.base(something);
delete this.base;
};

api.SubClass.prototype.__proto__ = api.BaseClass.prototype;

// here be subclass prototypes...

new api.SubClass('argument');实例化时发生错误实例没有获取函数“someFunction”。有人可以指导我如何正确地进行即使在 IE 中也能工作的继承吗?

最佳答案

这是因为 IE 不支持 __proto__
你应该这样做:

api.SubClass.prototype = new api.BaseClass();

关于javascript - JavaScript 中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6042565/

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