Reputation: 1277
Is there a way to do something like
class AppContainer {
fetch = _.debounce(...) {
}
}
I need to have a class method aliased to the lodash debounce function.
Upvotes: 3
Views: 2450
Reputation: 161627
You can define the method separately and then debounce it manually in the constructor, like this:
class AppContainer {
constructor(...args){
super(...args);
this.fetch = _.debounce(this.fetchImpl_.bind(this));
}
fetchImpl_(){
}
}
Upvotes: 2
Reputation: 4603
Add it to the prototype as you normally would. Quote:
JavaScript classes introduced in ECMAScript 6 are syntactical sugar over JavaScript's existing prototype-based inheritance
Demo:
// Define a class using the pre ECMA-6 mechanism
function Wonk(n) {
this.name = n;
}
// Add a function to the prototype of our pre ECMA-6 class
Wonk.prototype.wonk = function() { console.log( "Funky" ); };
// Create an instance of our pre ECMA-6 class as a stand-in for lodash's _
var wonk = new Wonk();
// Define an ECMA-6 class
class Funky {
constructor() {
}
}
// ECMA-6 class definition does not support (this claim needs a reference) adding a function by reference in the definition, so add it with the pre ECMA-6 mechanism
Funky.prototype.foo = wonk.wonk;
// Test the resulting class.
function init() {
var funky = new Funky();
funky.foo();
}
document.addEventListener("DOMContentLoaded", init, false);
Upvotes: 1