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 3235970
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:34:04+00:00 2026-05-17T17:34:04+00:00

Using jQuery, this is the format of most of the classes I write in

  • 0

Using jQuery, this is the format of most of the classes I write in JS:

$.Tabs = function(options){
var self = this;
this.defaults = {};
var cfg = $.extend(true, {}, this.defaults, options);
this.$elem = cfg.$elem;

function _init(){
    var dontInit = false;

    if (!cfg.$elem || !cfg.requiredProperty){
        alert('missing params');
        dontInit = true;
    }

    if (!dontInit){
        self.$elem.bridge(self);
    }
}

this.show = function(){};
this.hide = function(){};

_init();
}

The code above is just an example by the way.

My question is, how do you extrapolate common tasks/functionality from your classes, so every new class you write, you don’t have to retype everything again?

I created a Class creation script based off of John Resig’s Simple Javascript Inheritance pattern, which allows you to require certain parameters, bind event listeners for a pub/sub pattern, automatically combine defaults with options passed in, and it creates a bridge between the DOM element and the class instance (i.e $(‘elem’).data(‘instanceNamespace’, classInstance);)

But I’m uncomfortable with continuously using this class script to write my classes because it makes them less portable. But yet, I don’t want all my classes to have so many similarities between them when it feels like those aspects can be abstracted.

Thoughts? Or how do you format your Javascript classes?

Thanks in advance.

Edit: Also, would anyone know the impact using a class creation script has on performance? I’m assuming the extra closures adds overhead but is it enough to make it not worthwhile to use?

  • 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-17T17:34:05+00:00Added an answer on May 17, 2026 at 5:34 pm

    I think the idea you are looking for is called a Mix-in.

    Note: what follows is just vanilla javascript, no jQuery, but I think you’ll see that prototype inheritance is very well suited to the idea of mixins.

    Let’s say you define a simple constructor:

    var Person = function(name){
        this.name=name;
        this.sayhello = function(){
            console.log(this.name + ' says hello');
        };
    };
    

    Then we can mixin new abilities by copying the properties over to the new object with the function below:

    var extend = function(destination, source) {
        for (var property in source)
            destination[property] = source[property];
        return destination;
    };
    

    We can create some useful abilities that we may want to re-use for lots of different classes, so instead of having them all inherit from a common class, we can just mixin the new abilities at runtime:

    var DanceMixin = {
        dance: function(){
            console.log(this.name + ' is dancing!');
        }
    };
    
    var SingMixin = {
        sing: function(){
            console.log(this.name + ' is singing!');
        }
    };
    
    var bob = new Person('bob'); 
    extend(bob, SingMixin);
    extend(bob, DanceMixin);
    
    bob.sing();
    /* bob is singing! */
    bob.dance();
    /* bob is dancing! */
    bob.sayhello();
    /* bob says hello */
    

    Note that this is adding the new abilities to only a single object. If we wanted to add these abilities to all Person objects, we would need to modify the Person prototype.

    Now let’s say we have another trivial constructor for Animals:

    var Animal = function(name){
        this.name = name;
        this.roar = function(){
            console.log(this.name + ' is roaring!');
        };
    };
    

    And let’s say we want all of our animals to have the ability to dance. So we can add the DanceMixin to the Animal prototype using a function such as:

    var mixin = function(dest, src){
        for (var property in src)
            dest.prototype[property] = src[property];
        return dest;
    }
    mixin(Animal, DanceMixin);
    

    Now we can make some animals that can all dance…

    var lion = new Animal('lion');
    lion.dance()
    /* lion is dancing! */
    

    By the way, I highly recommend you check out the MooTools approach to the problem, which is quite elegant. Here is an example of using Implements in MooTools classes to facilitate mixins.

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

Sidebar

Related Questions

jQuery.fn.daterangepicker = function(settings){ var rangeInput = jQuery(this); //defaults var options = jQuery.extend({ ........ //function
Using this jquery code: <a href=javascript:void(0) id=m1>Get Selected id's</a> jQuery(#m1).click( function() { var s;
I'm using this: jQuery('.class1 a').click( function() { if ($(.class2).is(:hidden)) { $(.class2).slideDown(slow); } else {
Im using this jquery autocomplete plugin. The data format for autocomplete is like the
I have a date returned in this format YYYY-MM-DD, e.g. 2011-04-29. Using jQuery how
http://keith-wood.name/countdown.html I am using this jQuery plugin. $('#noDays').countdown({ until: enddate_final, format: 'HMS', compact: true,
What time format is this and how can I reformat it using JQuery? It's
I need to change an element's ID using jQuery. Apparently these don't work: jQuery(this).prev(li).attr(id)=newid
I'm using JQuery for this. The problem is that, when I drag an object
[edit] I am NOT using jquery in this app. Looking for a way to

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.