In javascript, everything is in the form of function(class, methods). There are multiple occasions where you wanted to write the function with the different number of arguments but with the same name that's what is called as function/method "Over Loading".
The best way to implement the function overloading is to use the object as an argument which will be very useful to write the code and use additional parameters without changing the much more definition of the function. Rather than using the arguments length to determine which function definition to be called.
For example, consider the find function which can possess with the different definition based on the criteria of search.
The best way to implement the function overloading is to use the object as an argument which will be very useful to write the code and use additional parameters without changing the much more definition of the function. Rather than using the arguments length to determine which function definition to be called.
For example, consider the find function which can possess with the different definition based on the criteria of search.
function find(){
var onlyFind = function(){
// find all the items without any criteria
}
var findByName = function(name){
// find all the items by their name
}
if(arguments.length == 0){
onlyFind.apply(this, arguments)
}else if(arguments.length == 1){
findByName.apply(this, arguments)
}
}
Instead of above, This situation can be handle by the object as an argument. Like if argument is an object(blank no key => {}) then find the elements without and criteria and if the name is the key inside the object then use it in the search criteria. In javascript type of the parameter is not considered unless it is specified by the user as custom criteria.
No comments :
Post a Comment