javascript - JavaScript 中的继承

标签 javascript internet-explorer inheritance

我正在将 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/

相关文章:

javascript - Math.floor之类的东西

html - 为什么 Internet Explorer 不将 CSS 应用于 "invalid"元素?

css - 如何使图像缩小以适合固定位置的 flexbox 模态

javascript - SharePoint 列表项 : Copy list-item-level permissions from one item to another

C++ 重载运算符,解引用引用

javascript - 页面加载时隐藏打印时显示

javascript - 如何在 ES6 Map 中中断/中断/停止 forEach?

c# - 从继承自 UIVIewcontroller 的类继承(xamarin 和 c#)

javascript - ReactDOM.render在特定情况下没有效果?

css - Google Autocomplete pac-icon 在 IE 中模糊