Mariner Mariner - 1 year ago 39
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:


as well as


Answer Source

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

function log(message) {
log.error = function(message) {