Let there be two objects: A and B.
A has a method: A.methodA(), and B also has a method: B.methodB().
Given this situation, please consider the below codes.
(1) Using other object's method directly
methodA() {
B.methodB();
}
A.methodA(); // B.methodB invoked
(2) Using event to call other object's method
window.addEventListener('call', callcallback);
function callcallback() {
B.methodB();
}
methodA() {
window.dispatchEvent('call');
}
A.methodA(); // B.methodB invoked
Both ways will result in calling B.methodB(), but I wonder what is the difference. Also I would like to know which way is better. (Or does each way depend on a situation which it is used?)