简体   繁体   English

不确定如何将方法定义转换为ES6

[英]Unsure how to convert method definition to ES6

I'm currently in the process of converting our Backbone application to ES6 syntax like eg this: 我目前正在将Backbone应用程序转换为ES6语法,例如:

action: function(e){},

Becomes

action(e) {}

However, now I'm having this code: 但是,现在我有了这个代码:

throttleEvent: _.throttle(function(e) {
    //do stuff
    }, 500);
}

And I can't seem to find how to convert this to valid syntax. 我似乎无法找到如何将其转换为有效的语法。 I tried 我试过了

throttleEvent _.throttle((e) => {
    //do stuff
    }, 500);
}

And

throttleEvent() {
    return _.throttle((e) => {
    //do stuff
    }, 500);
}

But these all failed to work. 但这些都无法奏效。

Help converting this to the valid syntax would be appreciated. 帮助将其转换为有效语法将不胜感激。

Well I'm not quite sure if the short syntax is applicable for your example. 好吧,我不太确定短语法是否适用于您的示例。

Lets have a look at your start 让我们来看看你的开始

action: function(e){},

you have an object, that object has a property called "action" and that property holds a function that later then can be called by obj.action(). 你有一个对象,该对象有一个名为“action”的属性,该属性包含一个函数,以后可以由obj.action()调用。

Now to your example 现在举个例子

throttleEvent: _.throttle(function(e) {}

Again you have an object, that has a property called throttleEvent. 你有一个对象,它有一个名为throttleEvent的属性。 But the main difference is the value. 但主要的区别在于价值。 The value is the return Value of the function _.throttle(). 该值是函数_.throttle()的返回值

Taking from the documentation ( https://lodash.com/docs/4.17.4#throttle ) 摘自文档( https://lodash.com/docs/4.17.4#throttle

Creates a throttled function that only invokes func at most once per every wait milliseconds 创建一个限制函数,每个等待毫秒最多只调用一次func

So your property actually holds the function that you got returned from the library. 因此,您的属性实际上包含您从库中返回的函数。 That explains why your second approach does not work. 这就解释了为什么你的第二种方法不起作用。 Because in this version every time you call the object property, you create a new throttle function. 因为在每次调用object属性时都会在此版本中创建一个新的节流函数。 (And to your first solution I think this is invalid syntax) (对于你的第一个解决方案,我认为这是无效的语法)

I think the best approach if you really want to use the short syntax is, assigning the throttle function to a variable before and then use it 我认为如果你真的想要使用短语法,那么最好的方法是,先将节流函数分配给变量然后再使用它

For example something like this 例如像这样的东西

const throttleFunction = _.throttle((event) => {
    //do stuff
}, 500);
const obj = {
    throttleEvent(event): any {
        return throttleFunction(event);
    },
};

But then it is to decide whether the ES6 syntax makes sense in this case or if you just stick with your original version 但是接下来要确定ES6语法在这种情况下是否有意义,或者只是坚持使用原始版本

throttleEvent: _.throttle(function(e) {
    //do stuff
    }, 500)

Just because there is a different syntax available, does not mean you always have to use the new one. 仅仅因为有不同的语法,并不意味着你总是必须使用新的语法。 In some cases the "old one" makes actually more sense 在某些情况下,“旧的”实际上更有意义

试试这个语法:

_.throttle(() => //Do stuff, 5000) 

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

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