Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
109 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


"Style-wise I like the Object Literal Notation because it provides enclosure."

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...

var TextProcessor = new Number();
var TextProcessor = new Boolean();
var TextProcessor = new Date();

...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, or Date has little relevance to the task at hand.

That's effectively what you're doing when you assign the properties to a Function object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...