Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Functions: Function Properties

Monday, August 6, 2018

JavaScript Functions: Function Properties

Since JavaScript functions are objects they can have properties that belongs to the function itself.  For example this simple example counts the number of times the function is being called by incrementing the counter property that belongs to the function.


    <script>
        functionCount.counter = 0;

        function functionCount() {
            return functionCount.counter++;
        }

        for (var i = 0; i < 10; i++)
            functionCount();

        console.log(functionCount.counter);

    </script>

The code above basically intialize the counter functionCount property and calls the function ten times and then output the counter property directly through the functionCount object, which is ten.


1 comment: