1
\$\begingroup\$

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 works as expected, and 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.

jsFiddle (the results are just in the console FYI)

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();
\$\endgroup\$
1
\$\begingroup\$

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, such as:

  • 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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