Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

javascript - Function vs. Object Literal Notaion - is there a diifference?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...