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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:00:28+00:00 2026-06-06T05:00:28+00:00

I want to create a Javascript class/object that allow me to have various method:

  • 0

I want to create a Javascript class/object that allow me to have various method:

Model class

  • Model.all() » static method
  • Model.find() » static method
  • Model delete() » instance method
  • Model save() » instance method
  • Model.create() » static that returns a new Model instance

For static method I can define them using:

Model.staticMethod(){ method }

while for instance method is better to use:

function Model(){
    this.instanceMethod = function(){}    
}

and then create a new instance

or using prototype?

var m = function Model(){

}

m.prototype.method() = function() {
}

Now let’s say that I want to create a new class based on Model, how to inherit not only its prototypes but also its static methods?

EDIT:

to avoid confusion this is more or less what I want to create:

http://activejs.org/activerecord/index.html and http://activejs.org/activerecord/ActiveRecord/Model/index.html

where I can define a new model using something like that

var User = ActiveRecord.create({
    username: '',
    password: '',
    post_count: 0,
    profile: ''
}

then create an instance

var jessica = User.create({
    username: "Jessica",
    password: "rabbit"
});

use instance methods like

jessica.save();

but also class methods:

User.findByUsername('Jessica');
  • 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-06-06T05:00:29+00:00Added an answer on June 6, 2026 at 5:00 am

    Commented code is pseudo code. You can achieve the same by:

    var modelInstanceMethods = {
    
        save: function() {
            /*
            insert into this.tableName blabla
            */
        },
    
        'delete': function() {
            /*
            delete from this.tableName blabla
            */
        }
    };
    
    var modelStatics = {
    
        create: function(obj) {
            return new this(obj);
        },
    
        all: function() {
            /*
            return select * from this.tableName.map( function( values ) {
                return new this(values);
            },this);
            */
        },
    
        find: function(id) {
            /*
            select * from this.tableName where id = id
            return new this(columnValues);
            */
        }
    
    
    };
    
    var ActiveRecord = {
        create: function( tableName, fields, methods ) {
    
            function Model( obj ) {
                this.tableName = tableName;
                this.fields = {};
                if( fields ) {
                    for( var field in fields ) {
                        this.fields[field] = fields[field];
                    }
                }
    
                if( obj ) {
                    for( var field in obj ) {
                        this.fields[field] = obj[field];
                    }       
                }
            }
    
            Model.tableName = tableName;
            Model.prototype = Object.create(modelInstanceMethods);
            Model.prototype.constructor = Model;
    
            for( var key in modelStatics ) {
                Model[key] = modelStatics[key];
            }
    
            if( methods ) {
                for( var key in methods ) {
                    Model.prototype[key] = methods[key];
                }   
            }
    
            return Model;
        }
    };
    

    Usage

    var User = ActiveRecord.create('users',
        /* fields and their default values */
    {
        id: 0,
        username: '',
        password: '',
        post_count: 0,
        profile: ''
    }, {
        /*instance methods */
        setPassword: function(password) {
            this.fields.password = password;
        }
    });
    
    /*You can define static methods like this */
    
    User.findByUsername = function(username) {
        /*select from this.tableName where userName = username
                   return new this(columnValues) blabla
                 */
    };
    
    var jessica = User.create( {
        username: "Jessica",
        password: "rabbit"
    });
    
    jessica.save();
    
    User.findByUsername('Jessica');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I want to create a javascript class/object/function which have a method that
I want to create a string in JavaScript that contains all ascii characters. How
I have the following HtmlHelper method that I want to create a button that
I want to know how to create a private attribute in a Javascript class.
I want create a new JavaScript object based on an existing JavaScript object. Till
Basically I want to create one large object of many object in JavaScript. Something
JavaScript. I want to create simple script, that will be resize loaded image using
Suppose I create a custom object/javascript class (airquotes) as follows: // Constructor function CustomObject(stringParam)
I want to create a simple class in Javascript with only one public function
I created a JavaScript object, but how I can determine the class of that

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.