Critique wanted on JS

I'm trying to re-learn some JS fundamentals and wondered if anyone knowledgabe could tell me if I'm doing something very wrong in my approach to this simple word counting class.

I want to create a class that takes arbitrary text and returns information about its wordcount, frequency of words, etc. One of the methods I have so far returns a hash where each key coresponding to a word has an element called count which is its frequency in the text.

One usage is to produce an output that will be printed to a file with WORD COUNT on each line.

function WordCounter(){
this.text = new String()
}

this.text += words
}

WordCounter.prototype.count = function(){
return this.text.split(" ").length
}
WordCounter.prototype.getFrequenciesAsHash = function(){
var words = this.text.split(" ")
var frequencies = {}
while(words.length > 0){
var word = words.pop()
if(typeof frequencies[word] == "undefined"){
frequencies[word] = {}
frequencies[word].count = 0
}
frequencies[word].count++
}
return frequencies
}

• I am not sure what you are trying to do really... Jul 17, 2013 at 21:11
• Thanks for looking. I guess I could have described better. I want to create a class that takes arbitrary text and returns information about its wordcount, frequency of words, etc. One of the methods I have so far returns a hash where each key coresponding to a word has an element called count which is its frequency in the text. Jul 17, 2013 at 21:14
• @maninjeans: Go ahead and edit that into the question for everyone else to see. Jul 17, 2013 at 21:16
• @jamal man you guys are fast, edited in as you typed :) Jul 17, 2013 at 21:18
• @maninjeans: I have a lot of free time. :-) Jul 17, 2013 at 21:20

Conceptually, I feel like you are doing this backwards. You should be storing an array of strings instead of repeatedly splitting a string into an array.

Here's my take on it:

function WordCounter(){
this.words = []; // You should just use an array. It makes more sense here.
}

// this allows you to pass in words as multiple arguments, or as an array of words
// i.e.:
// or
this.words = this.words.concat([].slice.call(arguments));
}
WordCounter.prototype.count = function(){
return this.words.length;
}
WordCounter.prototype.toString = function(){
//this replaces the text property
return this.words.join(" ");
}
WordCounter.prototype.getFrequenciesAsHash = function(){
// reduce is much more elegant here.
// if you have to support older browsers, just use a shim to fill in array method support
return this.words.reduce(function(hash, word){
// no need to do a typeof.
if(hash[word] === undefined)
hash[word] = 0; // no reason to create an object

hash[word]++;

return hash;
}, {});
}


If you want to be able to add words from a multi-word string, I would suggest a separate method for clarity:

WordCounter.prototype.extractWords = function(string){