An alternative to solve your problem would be to use overhead.
Ex: Overload - Number of parameters
Once again, I'll use Js's "apply" method to solve this, because the language messes a lot with prototypes and builders, nothing smarter than using this scope method. The idea I had was to use a function as a function and at the same time as an object that holds its own properties.
var Person = {
Class: function(){
//Private
var name = ''
//Public
this.name = function(){
return this.name[ arguments.length ].apply( this, arguments )
}
this.name[0] = function(){ return name }
this.name[1] = function(n){ name = n }
this.name[2] = function(a, b){ name = a + " " + b }
}
}
Here I create a function named "name" and create 3 more properties of it: 0, 1 and 2. They are numbers, but still are properties of the name method. What the main function will do is to see how many parameters it has and will call its corresponding property, which is nothing other than another method that will be executed in the same context as the name and will have the same parameters as the name. >
Logo:
var Edu = new Person.Class
Edu .name() // ''
Edu .name('Eduardo')
Edu . name() // "Eduardo"
Edu . name('Eduardo', 'Ottaviani')
Edu . name() // "Eduardo Ottaviani"
It's not because I had this idea, but I consider this the most elegant form of operator overload so far. Maybe it was not the most efficient, I did not test its speed in relation to the other techniques. If it is slower, I do not think it is noticeable.
Source: link