3 Powerful JavaScript Methods You Probably Didn’t Know About
<p>Traditionally in JavaScript, an object has it's own properties and methods/functions limited to it, ie an object cannot access another object's methods and vice versa. But with the use of the three functions I'll be sharing today, we can bypass this restrictions. These functions allows us to access another object's methods from an object as if it were it's own. We'll go through the functions one after the other with use Case. These functions includes;</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/0*mHDElxW0JdOb7-5d" style="height:467px; width:700px" /></p>
<p>Photo by Christopher Robin Ebbinghaus on Unsplash</p>
<h1>The Call() Method</h1>
<p>The call() method is used when you have multiple objects with a similar method within them, so rather than rewriting the method each time for all the objects, you'll write the function once like a regular function but in a way that is like a normal object method. Example,</p>
<pre>
const myObjOne = {num: 3}
const myObjTwo = {num: 5}
function addNum (a) {
return this.num + a
}
addNum.call(myObjOne, 3) //6
addNum.call(myObjTwo, 3) //8
</pre>
<p>Another use case of the call() method is using it to invoke an anonymous function.</p>
<p>Assume we have an array of objects, We're going to create an anonymous function and use the the call method to invoke it for each object in the array.</p>
<p><a href="https://medium.com/@cyberohn/3-powerful-javascript-methods-you-probably-didnt-know-about-48136cd8964a">Click Here</a></p>