Added binding of methods

This commit is contained in:
Francisco Hodge
2019-03-06 19:31:07 -05:00
parent abacee54ee
commit 00f8daff3c
2 changed files with 15 additions and 30 deletions
+14 -8
View File
@@ -14,14 +14,7 @@ class Utilities {
/**
* Bindings
*/
this.getButtonClass = this.getButtonClass.bind(this);
this.getButtonDisplayName = this.getButtonDisplayName.bind(this);
this.getUpdatedInput = this.getUpdatedInput.bind(this);
this.updateCaretPos = this.updateCaretPos.bind(this);
this.updateCaretPosAction = this.updateCaretPosAction.bind(this);
this.isMaxLengthReached = this.isMaxLengthReached.bind(this);
this.camelCase = this.camelCase.bind(this);
this.countInArray = this.countInArray.bind(this);
Utilities.bindMethods(Utilities, this);
}
/**
@@ -372,6 +365,19 @@ class Utilities {
return Boolean(this.maxLengthReached);
}
/**
* Bind all methods in a given class
*/
static bindMethods(myClass, instance) {
for (let myMethod of Object.getOwnPropertyNames(myClass.prototype)) {
let excludeMethod = (myMethod === "constructor" || myMethod === "bindMethods");
if (!excludeMethod) {
instance[myMethod] = instance[myMethod].bind(instance);
}
}
}
/**
* Transforms an arbitrary string to camelCase
*