5
Personal how to manage to emulate overload of methods in javascript
?
I know that some programmers can, for example, I will use Gulp.
Gulp has the method .task
:
var gulp = require('gulp');
gulp.task('example-task', function(){
console.log('running example-task')
})
gulp.task('example-task-2', ['example-task'], function(){
console.log('running example-task')
})
but if I want to run some tasks
before others, I do this:
var gulp = require('gulp');
gulp.task('example-task', function(){
console.log('running example-task')
})
gulp.task('example-task-2', ['example-task'], function(){
console.log('running example-task')
})
when I want to rotate the example-task-2
, I spin before the example-task
,
because the example-task-2
is a kind of task
dependent on example-task
.
Anyway, but this was used only to illustrate what I want to understand.
Notice the signature of the method .task
The first parameter is a string
, and the second can be an array or an anonymous function.
How do I get that? Since I come from JAVA, that would be easy: I would write two methods
with the same name and would change only the received parameter, would be something like this:
public interface Gulp {
public void task(String taskName, AnonymousFunction run);
public void task(String taskName, List<Task> depencencies, AnonymousFunction run);
}
and in js, as it would be?
Boy, business is nothing trivial then.. Besides a different number of parameters, sometimes we need to filter via types.
– user155542
@user155542 Yes. It is a matter of not creating too many possibilities not to become complex to manage the code.
– Sergio