Webolution Designs

Apply

Share This Post

The “apply” method to run a function is similar to “call”. The difference is that with “apply”, we can pass arguments to the function for the new context. Here is an example…

let animal = {
    animalName: ‘Bongo’,
    getName: function(param){
        return param + this.animalName;
    }
};
let newAnimal = { animalName: ‘George’ }; console.log(animal.getName.apply(newAnimal, [‘Animal Name: ‘]));

Notice that here we are passing an array of arguments. In this case, it is a simple string. We also altered our function to accept the parameter arguments passed. This would output “Animal Name: George to the console.

Happy Coding!

Clay Hess

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Code

Curriculum Innovation or Confusion

It is an early Monday morning just prior to the beginning of the semester.  A faculty member walks into the institution where he teaches.  He

Code

Creating a Promise

Promises are used for asynchronous code. It is a temporary holder for a value that gets returned by an asynchronous call. For example, a call

Do You Want To Boost Your Business?

drop us a line and keep in touch

Excuse Our Mess! We are currently remodeling!

We are currently in the process of redesigning our site. We appreciate your patience as some areas of our site show chipping and paint peeling. We hope to have it sparkling soon!

small_c_popup.png

Learn how we helped 100 top brands gain success.

Let's have a chat