Skip navigation

You can add properties to an function in JavaScript. Those who new to JavaScript may get shock. Yes, functions can have properties. Also note, that property is defined outside of original function!!!

Here goes simple example

function displayMessage(message) {
  displayMessage.numTimesExecuted++;
  alert(message);
};
displayMessage.numTimesExecuted = 0;
var a = displayMessage('first object');
var b = displayMessage('second object');
var c = displayMessage('third object');
console.log(displayError.numTimesExecuted);  // 3
alert(displayError.numTimesExecuted);   // 3

 

Of course this is not a news for those who have object oriented JavaScript knowledge.

My goal here is to invoke interest to those don’t started learning OOPs in JavaScript.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: