It is best to create a function that adds functionality.
Imagine a function as follows
function Original(x,y)
{
return {x,y}
}
You want to add a quick mechanism to add to both values, a certain number, therefore you create another function that abstracts the process, first copies the output of the Original function and defines a more comfortable prototype
function EnvolturaSumar(i)
{
this.x += i
this.y += i
}
See the complete example:
function Original(x,y)
{
return {x,y}
}
function Envoltura(x,y)
{
if (this == window)
throw new Error('debe utilizarse el operador new')
Object.assign(this, Original(x,y))
}
function EnvolturaSumar(i)
{
this.x += i
this.y += i
}
Envoltura.prototype = Object.create(Object.prototype, {
sumar: {value: EnvolturaSumar}
})
var x = new Envoltura(2,3)
console.log(x)
x.sumar(20)
console.log(x)
x.sumar(-10)
console.log(x)