Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm not new to programming, but I am new to working with javascript (beyond copy/paste and simple edits).

I am building a one-page JS app similar to redditinvestigator.com, and my goal is for it to be 100% done in javascript. Coming from a PHP/Java background, I'm more used to working in class oriented languages, so I just want to get a sense if I am implementing them correctly in javascript.

My code below works as expected, I am just hoping to get some feedback on if there is anything I technically am doing wrong, or if there is a better way to do some of the things I am implementing.

Link to jsFiddle: http://jsfiddle.net/4ZD4U/ (the results are just in the console FYI)

and the code itself:

var Comment = function(obj) {
    if (obj) {
        this.data = obj.body;
        this.created_utc = obj.created_utc;
        this.downs = obj.downs;
        this.gilded = obj.gilded;
        this.id = obj.id;
        this.link_id = obj.link_id;
        this.link_title = obj.link_title;
        this.name = obj.name;
        this.subreddit = obj.subreddit;
        this.ups = obj.ups;
    }
};


Comment.prototype.getBody = function() {
    return this.data;
};
Comment.prototype.getCreated = function() {
    return this.created_utc;
};
Comment.prototype.getDownvotes = function() {
    return this.downs;
};
Comment.prototype.getUpvotes = function() {
    return this.ups;
};
Comment.prototype.getScore = function() {
    return this.ups - this.downs;
};
Comment.prototype.isGilded = function() {
    return this.gilded;
};
Comment.prototype.getID = function() {
    return this.id;
};
Comment.prototype.getLinkID = function() {
    return this.link_id;
};
Comment.prototype.getLinkTitle = function() {
    return this.link_title;
};
Comment.prototype.getName = function() {
    return this.name;
};
Comment.prototype.getSubreddit = function() {
    return this.subreddit;
};



var RedditInterface = function(username) {
    this.userName = username;
    this.commentCount = 0;
    this.submissionCount = 0;
    this.lastCall = 0;
};

RedditInterface.prototype.getUsername = function() {
    return this.userName;
};

RedditInterface.prototype.sendQuery = function(endpoint, after, func) {
    var curDate = new Date().getTime();
    var timeSince = curDate - this.lastCall;
    if (timeSince < 5000)
        return null;
    this.lastCall = curDate;

    var urlD = '';
    if (after === '')
        urlD = "http://www.reddit.com/user/" + this.userName + "/" + endpoint + ".json?limit=100&jsonp=?";
    else
        urlD = "http://www.reddit.com/user/" + this.userName + "/" + endpoint + ".json?limit=100&after=" + after + "&jsonp=?";

    $.ajax({
        url: urlD,
        dataType: 'json',
        success: func
    });
};

RedditInterface.prototype.loadAllComments = function() {
    var here = this;
    var intervalHandle = window.setInterval(function() {
        here.sendQuery('comments', commentAfter, function(data) {
            if (data.data.children.length === 0) { //We've loaded all the comments reddit will give us
                console.log("Comments loaded. Total: " + commentLog.length);
                window.clearInterval(intervalHandle);
            }
            for (var point in data.data.children) {
                var ret = new Comment(data.data.children[point].data);
                var accept = true;
                for (var i = 0; i < commentLog.length; i++) {
                    if (commentLog[i].name == ret.name) {
                        console.log(i + ' (' + commentLog[i].name + ') is equal to current (' + ret.name + '), ommitting.');
                        accept = false;
                        break;
                    }
                }
                if (accept && typeof ret.name !== 'undefined') {
                    commentLog.push(ret);
                    commentAfter = ret.name;
                }
            }
            console.log(commentLog);
        });
    }, 5500);
    return intervalHandle;
};

//Begin Logic
var commentLog = [];
var submissionLog = [];
var commentAfter = '';

var ri = new RedditInterface("hueypriest");
var handle = ri.loadAllComments();
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Your code is very clean, easy to understand and well written. My only comment for improving it would be to take advantage of a framework that does some of this lifting for you.

Some Recommendations:

  • KnockoutJS
  • BackboneJS
  • AngularJS

As well, coming from an OO background, you might be interested at looking at Coffeescript, which is a higher level language that provides class constructs and such that compiles down into regular Javascript.

I'd post links but I apparantly need more reputation, but a quick google search will bring any of them up.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.