简体   繁体   English

在 JavaScript 中创建类 onclick 的新实例的正确方法是什么?

[英]What is the proper way of creating a new instance of a class onclick in JavaScript?

Class Example:类示例:

$(document).ready(function(){

    var $ = function(id)
    {
        return document.getElementById(id);
    }

    class someClass
    {
        constructor()
        {
            ...
        }
        someMethod()
        {
            ...
        }
    }
... // rest of examples within this scope
});

So far I am able to create an instance of the class object when the window loads and then calling a method of that class on a button click event while also binding this :到目前为止,我能够创建该类对象的实例,当窗口加载,然后调用这个类的方法上的一个按钮点击事件,同时还结合this

var obj = new someClass()
$('startButton').onclick = obj.someMethod.bind(obj)

All works fine and well until I want reset by deleting and creating a new instance of that class object.一切正常,直到我想通过删除和创建该类对象的新实例来重置。 I have attempted a couple of different methods:我尝试了几种不同的方法:

First, I attempted to call a function on button click that does one more task than before (instantiates a new object).首先,我尝试在单击按钮时调用一个函数,该函数执行的任务比以前多(实例化一个新对象)。 I tried this both with declaring the variable obj in the global scope specifying var for the type and assigning it to null , and then attempted to re-assign it and bind this on button click.我想这都与声明变量obj在全球范围内指定var的类型,并将其分配给null ,然后尝试重新分配,并绑定this对按钮的点击。 This works up until I attempt to call my setup() method:这一直有效,直到我尝试调用我的setup()方法:

$('startButton').onclick = function() {
    var obj = new someClass();
    var obj.setup.bind(obj); // fails to call
}

I then attempted another route, which more or less landed me in the same spot:然后我尝试了另一条路线,这或多或少让我落在了同一个地方:

$('startButton').addEventListener('click', function(event) {
    var obj = new someClass();
    console.log('obj is an instance of someClass?', obj instanceof someClass); // returns true
    obj.setup.bind(obj); // fails to call
});

Without creating a new method within someClass that manually resets all of my class attributes back to the initial values (I'd rather re-call the constructor), how can I gracefully instantiate a new class object on button click and be able to bind this ?如果没有在someClass中创建一个新方法来手动将我的所有类属性重置回初始值(我宁愿重新调用构造函数),我如何在按钮单击时优雅地实例化一个新类对象能够绑定this ?

And when I do, is it okay to re-assign the variable holding the class object without first marking it for GC or deallocating it somehow?当我这样做时,是否可以重新分配保存类对象的变量而不先将其标记为 GC 或以某种方式解除分配? Nothing else references it.没有其他内容引用它。 Apologies in advance if my terminology is off, I'm new to this.如果我的术语关闭,请提前道歉,我是新手。

obj.setup.bind(obj) binds a context to a function, but it does not call it. obj.setup.bind(obj)将上下文绑定到函数,但不调用它。 You either need to call it:你要么需要调用它:

obj.setup.bind(obj)();

Or use .call() instead of .bind() :或者使用.call()而不是.bind()

obj.setup.call(obj);

However in this case, since you call the method directly on the instance, there is not really a need to bind anything:但是在这种情况下,由于您直接在实例上调用该方法,因此实际上不需要绑定任何东西:

 $(document).ready(function() { var $ = document.getElementById.bind(document); // <-- bind without calling class someClass { constructor() { this.ready = false; } setup() { this.ready = true; } } $('startButton').addEventListener('click', function(event) { var obj = new someClass(); obj.setup(); console.log(obj.ready); // true }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="startButton">Start</button>

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

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