Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4584160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:18:57+00:00 2026-05-21T21:18:57+00:00

Sometimes, using jQuery induces you to abuse its power (at least for me because

  • 0

Sometimes, using jQuery induces you to abuse its power (at least for me because of its selector matching capability). Event handlers here and there. Utility functions here and everywhere. Code coherence can almost seem nonexistent. I want to alleviate that problem by implementing OOP patterns, but since I have C++ and python background, implementing it in javascript is weirding me out a little bit.

The code below uses OOP patterns, but I’m not entirely sure if my implementations are good practices. The reason I’m doubting my implementations is because of the 3rd comment in my last stackoverflow question. I know it’s only one certain detail in my code he commented on, but it also makes me wonder about the other patterns I’m implementing in my code.

I would really appreciate if you could point out the flaws and pitfalls in my patterns and/or if you have any suggestions. Many thanks in advance.

(this code is an simplification of something I’m developing, but the idea is similar)

Live Example

$(function(){
    var stream = new Stream();
});


/*          Stream Class
------------------------------------------*/
function Stream(){

    // Disables multiple Stream objects
    if (this.singleton)
        return
    else
        this.__proto__.singleton = true;


    this.elements = jQueryMapping(this.selectors) // Converts a map of selectors to a map of jQuery objects
    this.initEvents();
}

Stream.prototype.singleton = false;

Stream.prototype.selectors = {
    stream : '#stream',
    post_content : '#post-content',
    add_post: '#add-post',
    // ... more action selectors
}

Stream.prototype.initEvents = function(){
    this.elements.add_post.click(this, this.addPost);
    // ... more action event-listeners
}

Stream.prototype.addPost = function(e){
    var self = e.data;
    var post_content = self.elements.post_content.val();

    if (post_content)
        self.elements.stream.append(new Post(post_content));
}


/*          Post Class
------------------------------------------*/
function Post(post_content){
    this.$element = $('<li>')
                        .text(post_content)
                        .append('<button class="delete-post">Delete</button>');

    this.elements = jQueryMapping(this.selectors, this.$element);
    this.initEvents();

    return this.$element;
}

Post.prototype.selectors = {
    delete_post: 'button.delete-post',
    // ... more action selectors
}

Post.prototype.initEvents = function(){
    this.elements.delete_post.click(this.deletePost);
    // ... more action event-listeners
}

Post.prototype.deletePost = function(){
    $(this).parent().slideUp();
}


/*          Utils
------------------------------------------*/
function jQueryMapping(map, context){
    // Converts a map of selectors to a map of jQuery objects

    var $map = {};
    $.each(map, function(key, value){ 
            $map[key] = (context) ? $(value, context) : $(value);
    });

    return $map;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:18:58+00:00Added an answer on May 21, 2026 at 9:18 pm

    I believe your code is over engineered. I’ve re factored and it simplified it as can be seen here. If you really want a heavy OOP setup I recommend you use a clientside MVC (Backbone, knockout, etc) construct to do it properly or keep it light instead.

    I’ll proceed with general feedback on your code.

    /*          Stream Class
    ------------------------------------------*/
    function Stream(){
    
        // Disables multiple Stream objects
        if (this.singleton)
            return
        else
            this.__proto__.singleton = true;
    
    
        this.elements = jQueryMapping(this.selectors) // Converts a map of selectors to a map of jQuery objects
        this.initEvents();
    }
    

    There is no reason to use a singleton like this. It’s also very bad to use .__proto__

    I would recommend pattern like this instead.

    var Stream = (function() {
        var Stream = function() { ... };
    
        // prototype stuff
    
        var stream = new Stream();
    
        return function() {
             return stream;
        };
    })());
    

    Storing a hash of data like that on the prototype is unneccesary.

    Stream.prototype.selectors = {
        stream : '#stream',
        post_content : '#post-content',
        add_post: '#add-post',
        // ... more action selectors
    }
    

    You can include this as a defaults hash instead.

    (function() {
        var defaults = {
            stream : '#stream',
            post_content : '#post-content',
            add_post: '#add-post',
            // ... more action selectors
        }
    
        function Stream() {
             ...
    
             this.elements = jQueryMapping(defaults);
        }
    
    }());
    

    Your utility function could be optimised slightly.

    $map[key] = (context) ? $(value, context) : $(value);
    

    This could be rewritten as

    $map[key] = $(value, context)
    

    Since if context is undefined you just pass in an undefined paramater which is the same as passing in no parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jquery and creating event handlers like this: $('#some_selector a.add').live('click', function(){...}); However, I
I'm using jquery-plugin columnizer http://welcome.totheinter.net/columnizer-jquery-plugin/ It is sometimes very slow to create the columns
Using JQuery: Sometimes, I can do variable.val() and it works. Sometimes, I'm required to
I am using jQuery draggables/droppables bound to a live() mouseover event. I'm using live
I have PhoneGap app in Android using jQuery. Sometimes I need to upload some
OK here goes, I'm using jQuery ajax to load post from inside a slider.
I am binding the click event to 4 href's (they have class=alink) using JQuery
I'm using jQuery and jQuery UI. I experienced that users sometimes fire the ajax-calls
im using jquery ui tabs. sometimes when my page loads, all the html for
I am using jquery with ajax. Sometimes, It is giving error $ is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.