Friday, April 27, 2018

Function Object with Custom Properties

We can add our own custom properties with function object.

Below example will explain you How do we use custom property with function object.


function countTest() {
  alert("Hi");

  countTest.counter++;
}
countTest.counter = 0; // initial value

countTest(); // Hi
countTest(); // Hi

alert( `Called ${countTest.counter} times` ); // Called 2 times

Explanation :

We are calling countTest function at two times. So while calling at every time counter (custom property) will be executed and returning 2 in alert.


No comments:

Post a Comment