Published: 16 Feb 2018 › Updated: 16 Feb 2018

JavaScript Basics: Reflect.apply
The method Reflect.apply is just a better and more meaningful way to do Function.prototype.apply. In other words, it allows you to call the given function using a specified context (this argument) on an array of arguments. Here is an example:
let person = {
name: "John",
speak(city) {
console.log(`Hi. I am ${this.name}. I am calling from ${city}.`);
},
};
let someone = {
name: "Jack",
};
person.speak("Houston");
// Hi. I am John. I am calling from Houston.
Reflect.apply(person.speak, someone, ["Boston"]);
// Hi. I am Jack. I am calling from Boston.
person.speak.apply(someone, ["Boston"]); // the same
Function.prototype.apply.call(person.speak, someone, ["Boston"]); // the same
As I said in an earlier post, the Reflect object gathers all functionality related to reflection in one place. This makes it possible to write neater and more readable code.
Related Posts
Leave JavaScript Basics: Reflect.apply to:
Read more #javascript posts
Best Posts From ghasemkiani
We have not curated any of ghasemkiani's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From ghasemkiani
- Merging PDF Files with Java
- Happy Iranian New Year
- Stephen Hawking, one of the greatest physicists of our time, dies at 76
- Using Scoped NPM Packages
- Optimistic Typing in Nashorn
- JavaScript Basics: Reflect.apply
- JavaScript Basics: Reflect.construct
- JavaScript Basics: Reflect
- New Horizons Broke the Record of the “Pale Blue Dot” Picture
- JavaScript Basics: Object.freeze and Object.isFrozen
- JavaScript Basics: Object.seal and Object.isSealed
- JavaScript Basics: Object.preventExtensions and Object.isExtensible
- JavaScript Basics: Object.prototype.hasOwnProperty
- JavaScript Basics: Object.is
- JavaScript Basics: Object.keys, Object.values, and Object.entries
- What Is Success and How Do You Define It?
- Steemit Reached 700,000 Members
- JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf
- JavaScript Basics: Object.assign
- JavaScript Basics: Object.create