Métodos: call, apply & bind
Las funciones en JavaScript también son objetos, tienen propiedades y métodos. Algunos de los métodos más comunes son:
- call
- bind
- apply
call
Permite invocar una función con un valor this
asignado:
var square = {
length: 2
}
function getSquarePerimeter() {
console.log('Perimeter: ' + this.length * 4);
}
getSquarePerimeter.call(square); // Perimeter: 8
El método call puede aceptar cualquier número de argumentos adicionales que serán pasados a la función que está siendo invocada
var square = {
length: 2
}
function getSquarePerimeter(color) {
console.log('Perimeter: ' + this.length * 4 + ' Color: ' + color);
}
getSquarePerimeter.call(square, 'blue'); // Perimeter: 8 Color: blue
apply
Funciona de manera similar a call
. La diferencia es que en lugar de aceptar los argumentos adicionales uno por uno, basta con mandarle un arreglo con todos ellos:
function shapeStyle(fillColor, borderColor) {
this.fillColor = fillColor;
this.borderColor = borderColor;
}
shapeStyle.apply(square, ['blue', 'black']);
console.log(square); // Object {length: 2, fillColor: "blue", borderColor: "black"}
bind
Este método nos retorna una nueva función con el valor this
asignado:
var mySquare = getSquarePerimeter.bind(square);
mySquare('yellow'); // Perimeter: 8 Color: yellow