在特定的javascript类中声明多个函数

时间:2013-06-06 05:56:48

标签: javascript

我不明白为什么这不起作用,但是如果你注释掉countGrin函数,它的效果非常好:

var DetectEmoticons = {
    countHappy: function(data) {
        var pattern = new RegExp("[:=]-?[)|\\]]|[(|\\[]-?[:=]", "g");
        var count = (data.match(pattern) || []).length;
        return count;
    }
    countGrin: function(data) {
        var pattern = new RegExp("[:=]-?[D]", "g");
        var count = (data.match(pattern) || []).length;
        return count;
    }
}
alert(DetectEmoticons.countHappy("=) :) :] :[ ]="));
alert(DetectEmoticons.countGrin(":D =D"));

这是jsfiddle:http://jsfiddle.net/9BTAb/

1 个答案:

答案 0 :(得分:4)

也许你在第一个函数SEE FIDDLE

之后缺少,
var DetectEmoticons = {
    countHappy: function(data) {
        var pattern = new RegExp("[:=]-?[)|\\]]|[(|\\[]-?[:=]", "g");
        var count = (data.match(pattern) || []).length;
        return count;
    }, //put a comma here
    countGrin: function(data) {
        var pattern = new RegExp("[:=]-?[D]", "g");
        var count = (data.match(pattern) || []).length;
        return count;
    }
}