Skip to content

Instantly share code, notes, and snippets.

@Bryan-Herrera-DEV
Created April 10, 2022 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bryan-Herrera-DEV/3090206b9d6263e850829449af08ef1c to your computer and use it in GitHub Desktop.
Save Bryan-Herrera-DEV/3090206b9d6263e850829449af08ef1c to your computer and use it in GitHub Desktop.
JavaScript funciones: .call() .apply() .bind() simplificadas.

JavaScript funciones: .call() .apply() .bind() simplificadas.

const sayHi = function() {
  console.log(`Hi ${this.name}`)
}:

const person = {
  name = "foxy",
};

sayHi.call(person);
sayHi.apply(person);
sayHi.bind(person)();

.call()

const addItems = function(item1, item2){
    this.items.push(item1, item2);
};
const cart = {
    item: [],
};
addItems.call(cart, 'lettuce', 'watermelon');
const  addItems = function(){
	thsi.items.push(...arguments);
};	

.apply()

addItems.apply(cart, ['egg fried rice', 'cucumber']);

.bind()

const boundAddItems = addItems.bind(cart);
boundAddItems('milk', 'jellybeans', 'more jellybeans');

document.querySelector('bitton')
	.addEventListener('click', boundAddItems);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment