I tried to JS takes the object as a parameter and passes it to another constructor, the new object cannot get the method code is as follows
I have a constructor Operation, responsible for defining the user operation interface and put the real algorithm logic on the prototype chain (someThing function)
code:
const Operation = function (operationController) {
this.operationController = operationController;
}
Operation.prototype.someThing = function () {
console.log('It fails here!');
};
There is an object oneOperation which defines different operations, because there can be multiple operation objects, for example: twoOperation and threeOperation etc...
code:
const oneOperation = {
// I will get error doSomeThing is not a function
doSomeThing: function () {
this.someThing();
}
}
const aUserOperation = new Operation(oneOperation);
Finally, create an instance through the User constructor, then pass in the aUserOperation control operation, and monitor the event through addEventListener
code:
const User = function (operation) {
this.userOperation = operation;
this.initListenerOperation = function () {
window.addEventListener('keydown', function() {
oneUser.userOperation.operationController.doSomeThing();
});
}
}
const oneUser = new User(aUserOperation);
oneUser.initListenerOperation();
I will get error doSomeThing is not a function, Please help me, or there is a better way
Thanks