What is the purpose of call and apply methods in the language?
It seems to be an example of anti-pattern which breaks OOP principles. Or it just presents multi-paradigm approach?
This example made me to ask this question.
In one place of application (a part of library) there is such a code
socket.emit = function() {
if (!socket.banned) {
emit.apply(socket, arguments)
}
}
In another part of application (self written) there is such piece of code
socket.emit = function(arguments){
data = Array.prototype.slice.call(arguments);
emit.apply(socket, data);
}
In the first place the object should not work if it is banned. But the second part knows nothing about library logic and works independently.
How this situations may be resolved? We use libraries to ease the most difficult parts.
this, and any number ofarguments. Other than that, it's not really clear what you need to know. If this is a question about programming principles, maybe this is better suited for Software Engineering.argumentsfor your formal parameter? Doing so shadows theargumentsobject, since the identifierargumentsnow refers to the first argument of the function, instead of the default object calledargumentsthat has access to all arguments passed into the function. Do you expectemitto be called with an array-like object as its first parameter? If you didemit = function(firstArg){ ...andslice.call(firstArg);the functionality would be much clearer.