【发布时间】:2018-04-17 09:19:35
【问题描述】:
我正在使用 ES2015 Babel 转译器。
我遇到了一些非常奇怪的事情。实际的类很复杂。所以我将使用一个我实际上没有运行的示例。这是一个时间问题我不知道我是否可以用下面的代码完全复制它,但想法是这样的:
class A {
static instance() {
if (!A._instance) {
A._instance = new A();
}
return A._instance;
}
foo() {
console.log('A')
}
}
class B extends A {
static instance() {
if (!B._instance) {
B._instance = new B();
}
return A._instance;
}
foo() {
console.log('B')
super.foo();
}
}
class C extends B {
static instance() {
if (!C._instance) {
C._instance = new C();
}
return C._instance;
}
foo() {
console.log('C')
super.foo();
}
}
// Somewhere else
class User {
static bar() {
C.instance().foo(); // Sometimes, this calls B.foo() directly, C.foo() is bypassed!
}
}
// create A._instance first
A.instance().foo();
// now C will inherint _instance from B which inherits is from A.
User.bar()
我在一个普通的 Cordova 项目中使用 Gulp 运行 ES6 转译器。但是当我尝试在桌面 Chrome 中运行它时,同样的事情发生了。
有时C.instance().foo() 实际上并不调用 C 中定义的foo,而是调用B.instance().foo()。 “有时”是指当我加载登录页面并照常登录时,我可以在 Chrome 中 100% 复制它。但我永远无法复制它,如果选中了应用程序的Remember me 选项,并且用户直接登录到主页。这似乎是一个时间问题。但我不知道它到底是什么。有什么线索吗?
编辑 1: 通过在 index.html 中单独使用普通的旧嵌入脚本标签,将代码文件包含到项目中。
【问题讨论】:
-
我已将您的代码转换为可运行的示例。转译由 babel 完成。它没有显示所描述的错误。我建议您创建一个演示问题并发布转译代码的最小示例。
-
我添加了可能演示您所描述问题的可能场景。 :) 如果不只是恢复我的更改:)
-
单例已损坏。具有继承性的单例更加破碎。根本不要尝试这样做。
-
@YuryTarabanko 哇,非常感谢!我不知道我能做到这一点。
-
@Bergi 你说的坏了是指设计坏了?或者你的意思是它的语言被破坏了?很多时候我不知道还能用什么。
标签: javascript ecmascript-6 babeljs