简体   繁体   English

我们可以删除 ES6 类吗?

[英]Can we delete ES6 class?

Can we really delete a class create by es6 "class"?我们真的可以删除由es6“类”创建的类吗?

 class myClass{} delete myClass; console.log(myClass); //output: class myClass{} var myClass=0; //Uncaught SyntaxError: Identifier 'myClass' has already been declared

删除只会删除对象属性或全局变量。

https://stackoverflow.com/a/44694753/461412 https://stackoverflow.com/a/44694753/461412

this answer is useful.这个答案很有用。 yet not perfect.还不完美。

i guess you could never delete it after it's declared as an identifier, but you could wrap it into a deletable variable in the first place.我想在它被声明为标识符之后你永远不能删除它,但你可以首先将它包装成一个可删除的变量。

still i'm looking for a better solution.我仍然在寻找更好的解决方案。 our team is trying to build a detachable modules system.我们的团队正在尝试构建一个可拆卸的模块系统。 deadly, any declared class is not detachable.致命的是,任何声明的类都是不可分离的。 we don't like the idea of transpiling every module we load.我们不喜欢转译我们加载的每个模块的想法。

甚至 Array 是 Global 的一个属性。类不是。

This is what I used:这是我使用的:

window.MyClass = class extends SuperClass {} // in an external file

// load the file into a script tag, append it to the body, and execute the following after it loads:

alert(MyClass); // Outputs the class

delete window.MyClass;

alert(MyClass); // Outputs Uncaught ReferenceError: MyClass is not defined

It works fairly well.它工作得相当好。 Using this approach I can import .js files into my document using script tags, access the code from the window object, and then remove the script tag and remove the reference to the class.使用这种方法,我可以使用脚本标签将 .js 文件导入到我的文档中,从 window 对象访问代码,然后删除脚本标签并删除对类的引用。 I haven't done much testing on it, but it could be used to load custom logic for a single level in a game and then get rid of it, or something more useful perhaps.我没有对它做太多测试,但它可以用于加载游戏中单个关卡的自定义逻辑,然后摆脱它,或者更有用的东西。 Honestly, it feels really hacky to do this.老实说,这样做感觉真的很糟糕。

This is the closest I could get to "remove" a Constructor这是我最接近“删除”构造函数的方法

class MyClass { 
  constructor(a, b) {
    this.a = a;
  }
}

MyClass = null;            
console.log( MyClass );                   // -> null

var testObj = new MyClass_("val");        // -> TypeError

This does NOT delete the Constructor, it just changes the value of "MyClass" to null!这不会删除构造函数,它只是将“MyClass”的值更改为 null!

But for debugging purposes that'll do但出于调试目的,这会做

I've made a worker for resolving the deletion of import classes.我已经制作了一个解决导入类删除问题的工作者。

worker.js工人.js

onmessage = function(e) {
    let v = JSON.parse(e.data)
    ...
    import(`module.js?p=${v.query}`)
        .then(module => {
             let obj = new module.default(v.params)
             ...
             postMessage(obj.result)
        })

main.js主文件

async function doInBackground(x,y, output) {
    let r = await new Promise((resolve, reject) => {

        let w = new Worker('worker.js')
        w.postMessage(JSON.stringify({
            "x" : x,
            "y" : y
        }))
        w.onmessage = function(e) {
            resolve(e.data)
            delete w
        }
        w.onerror = reject
    })
    output.result = r
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM