【发布时间】:2012-04-09 20:38:28
【问题描述】:
这两种方式都使用相同的调用机制。
显然,我想使用最好的方式,但也许只是偏好问题?
就风格而言,我喜欢 Object Literal 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];
};
对象文字符号
/**
*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];
}
}
【问题讨论】:
-
一些关于如何使用这些对象的附加信息会很有帮助。是否只会创建其中一个对象,循环中的多个对象等?
-
您想知道哪种方法最适合创建类吗?还是想解决这种特殊情况?
-
我想要一个好的类(对象)层次结构......
-
我希望比我更聪明的人也能为此演示揭示模块模式。
-
我想我会用这个 - stackoverflow.com/questions/1595611/…
标签: javascript