Both these ways work using the same call mechanism.
Obviously, I want to use the best way, but perhaps it is just a matter of preference?
Style-wise I like the Object Literal Notation because it provides enclosure.
Function Notation:
var TextProcessor = function()
{
};
TextProcessor.unEscape = function( second_split )
{
var element;
for( element in second_split )
{
second_split[element] = second_split[element].replace( '**', '*', 'g' );
second_split[element] = second_split[element].replace( '|*', '|', 'g' );
}
return second_split;
};
TextProcessor.pullBullet = function( text )
{
var pattern = /<(.+)_([a-z]){1}>$/;
return pattern.exec( text );
};
TextProcessor.pullDomain = function( text )
{
return text.match( /:\/\/(www\.)?(.[^\/:]+)/ )[2];
};
Object Literal Notation
/**
*TextProcessor
*/
var TextProcessor =
{
unEscape: function( text )
{
var index;
for( index in second_split )
{
text[index] = text[index].replace( '**', '*', 'g' );
text[index] = text[index].replace( '|*', '|', 'g' );
}
return second_split;
},
pullBullet: function( text )
{
var pattern = /<(.+)_([a-z]){1}>$/;
return pattern.exec( text );
},
pullDomain: function( text )
{
return text.match( /:\/\/(www\.)?(.[^\/:]+)/ )[2];
}
}
You’re doing two somewhat different things.
The first example creates a function object and assigns properties to it.
The second example creates a plain object with those properties.
The first one really doesn’t make much practical sense in your example. You can use a function object to assign properties, but why would you? Those properties have no impact on the invocation of the function.
I don’t know what “enclosure” is. It sounds like a combination of encapsulation and closure, of which an object literal provides neither.
Getting back to the first part, imagine if you created any one of these objects…
…and then assigned the properties to it. It would work, but it would be an odd thing to do. The fact that the object is a
Number,Boolean, orDatehas little relevance to the task at hand.That’s effectively what you’re doing when you assign the properties to a
Functionobject.