Mariner Mariner - 3 months ago 8
jQuery Question

JavaScript member function and main function in the same pattern?

How can a JavaScript pattern be written such that a function, e.g. log, can be invoked in the following two ways?

log.error('this is an error message');
log('this is a standard message');





I am aware that jQuery can be invoked like:

$('div').show();


as well as

$.extend()

Answer

A function is an object, you can assign properties to it. Those properties can be functions as well

function log(message) {
   	console.log(message);
}
log.error = function(message) {
  	console.error(message)
}

log("Hello");
log.error("Error")