简体   繁体   English

当调用对象已经使用“ this”时,如何从静态方法中引用包含类?

[英]How do I reference the containing class from within a static method when “this” is already taken by the calling object?

Using ES6/ES2015 and webpack, I am trying to wrap my head around the little monster that is the keyword this . 使用ES6 / ES2015和webpack,我试图将自己的头缠在关键字this的小怪物上。

I have a class Edit containing a static method change(event) and within that method I am trying to make a recursive call to the method itself (depending on a given variable). 我有一个包含静态方法change(event) Edit类,在该方法中,我试图对方法本身进行递归调用(取决于给定的变量)。

In most cases I could just call this.change(event) but here the keyword this is already occupied by the jquery object that was calling the function instead of the containing class. 在大多数情况下,我可以只调用this.change(event)但是这里的关键字this已经被调用该函数而不是包含类的jquery对象占用。

The easiest solution would be to just call Edit.change(event) instead, but there must be a cleaner solution. 最简单的解决方案是只调用Edit.change(event) ,但必须有一个更干净的解决方案。 Every programming language I have encountered so far has had some reference to its containing class built in. 到目前为止,我遇到的每种编程语言都对其内置的包含类有所参考。

I promise I have checked the docs and other threads on stackoverflow, but no one I found seems to address this particular problem. 我保证我已经检查了stackoverflow上的文档和其他线程,但是我发现没有人似乎可以解决这一特定问题。

// main.js
'use strict';

const $ = require('jquery');
const Edit = require('./Edit');

$(document).ready(() => {
    let thingsToAddToData = {
        whatToDo: // defined here depending on context
        someVariable: //defined here depending on context
        };
    $('table :input').change(thingsToAddToData, Edit.change);
}

and here the Edit class is defined 在这里定义了Edit类

// Edit.js
class Edit {

    static change(event) {
        if(event.data.whatToDo === 'do_nothing'){
            return false;
        }
        if(event.data.whatToDo === 'do_this_important_thing'){
            // here some important stuff is done
            return true;
        }

        if(event.data.someVariable === 'special_case'){
            event.data.whatToDo = 'do_this_important_thing'

            // THIS IS THE LINE THAT GIVES ME HEADACHES
            return this.change(event);
        }
        // here some default stuff is done
    }
}

module.exports = Edit;

The easiest solution would be to just call Edit.change(event) instead, but there must be a cleaner solution 最简单的解决方案是只调用Edit.change(event) ,但必须有一个更干净的解决方案

No, this is indeed what you need to use to always refer to the Edit class. 不,这确实是始终引用Edit类所需要使用的。 There's nothing messy with it, just use it. 没什么麻烦的,只需使用它即可。

You could also use this.change(event) if you weren't using the method as an event handler. 如果未将方法用作事件处理程序,则也可以使用this.change(event) Make sure to call it as a method: 确保将其作为方法调用:

$('table :input').change(thingsToAddToData, Edit.change.bind(Edit));
// or
$('table :input').change(thingsToAddToData, e => Edit.change(e));

Either of the answers by @Bergi, should work (using Function.prototype.bind or () => {} ). @Bergi的任何一个答案都应该起作用(使用Function.prototype.bind() => {} )。 However I think your problem is more structural. 但是我认为您的问题更具结构性。 Since Edit.change is an event handler it doesn't make sense to call it directly, since it is supposed to be fired through events. 由于Edit.change是事件处理程序,因此直接调用它是没有意义的,因为它应该通过事件触发。

I would suggest firing the event again with some parameter changes ( http://api.jquery.com/trigger/ ): 我建议通过一些参数更改再次触发事件( http://api.jquery.com/trigger/ ):

replace Edit.change(event); 替换Edit.change(event); with this.trigger(event); this.trigger(event);

That way there is no need for calling the handler directly, and you don't need to change the this context, thus keeping the code more transparent. 这样,就不需要直接调用处理程序,也不需要更改this上下文,从而使代码更加透明。

Static methods operate on the class instead of instances of the class, they are called on the class. 静态方法在类上而不是类的实例上操作,它们在类上被调用。 There are two ways to call static methods: 有两种方法可以调用静态方法:

<ClassName>.methodName() 

or 要么

<class-instance>.constructor.methodName() 

In static methods, the this keyword references the class. 在静态方法中,this关键字引用该类。 You can call a static method from another static method within the same class with this. 您可以使用此方法从同一类中的另一个静态方法中调用一个静态方法。

暂无
暂无

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

相关问题 如何创建从类中引用方法的事件侦听器(鼠标单击)? - How do I create an event listener (mouse click) that will reference a method from within a class? ECMAScript 6-如何在类中传递对“自我”的引用? - ECMAScript 6 - How do I pass a reference to “self” from within a class? 如何在$ .post()调用中引用对象 - How do I reference the object from within a $.post() call 如何从对象内部引用方法 - how to reference a method from within an object 如果我在该类中另一个对象中的一个对象中,我如何获得对我所在类的实例的引用? - how do I get a reference to the instance of a class that I am in if I am in an object within another object within that class? 如何从同一对象的方法引用属性? - How do I reference property from method of the same object? 如何从类引用实例化javascript对象? - How do I instantiate a javascript object from class reference? 调用ES6方法时绑定上下文。如何从称为回调的方法中访问对象? - Binding context when calling ES6 method. How to access object from within method called as callback? 如何使用Dojo引用Google Maps事件中的包含类? - How do I reference the containing class from a Google Maps event using Dojo? 扩展THREE.js“类”时,为什么需要从新对象内调用.call(this)方法? - When extending a THREE.js “class”, why do I need to invoke the .call(this) method from within the new object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM