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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:26:13+00:00 2026-06-13T09:26:13+00:00

I am trying to make a Class factory with Javascript so that I can

  • 0

I am trying to make a “Class” factory with Javascript so that I can create different types of objects.

Here is the function I’m using:

var Class = function(methods) {   
    var klass = function() {
        var self = this;
        this.initialize.apply(this, arguments);
    };  

    for (var property in methods) { 
       klass.prototype[property] = methods[property];
    }

    if (!klass.prototype.initialize) klass.prototype.initialize = function(){};

    return klass;    
};

Then I can do:

var myObject = Class({
    initialize: function() { console.log(self);}
});
var createdObject = new myObject();

However, the console.log(self) is always referring to Window, and I’d like it to refer to the object itself.

I know this is a scope issue, but I’m confused on how to create a reference to the object?

I am trying to make a “Class” factory with Javascript so that I can create different types of objects.

Here is the function I’m using:

var Class = function(methods) {   
var klass = function() {
    var self = this;
    this.initialize.apply(this, arguments);
};  

for (var property in methods) { 
   klass.prototype[property] = methods[property];
}

if (!klass.prototype.initialize) klass.prototype.initialize = function(){};

return klass;    

};

Then I can do:

var myObject = Class({
    initialize: function() { console.log(self);}
});
var createdObject = new myObject();

However, the console.log(self) is always referring to Window, and I’d like it to refer to the object itself.

I know this is a scope issue, but I’m confused on how to create a reference to the object?

For example, if I wanted to do:

var myObject = Class({
    initialize: function() { 
       $('#myDiv').click( function() {
           self.anotherFunction();
       });
    },

    anotherFunction: function() {
        alert('hi');
    }
});

I would need to be able to reference the “myObject” with self…

  • 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-13T09:26:14+00:00Added an answer on June 13, 2026 at 9:26 am

    Use this instead of self. self will not be accessible to initialize function as it is defined outside the scope of klass self

    Best option is define self inside each function as last solution I provided.

    var myObject = Class({
        initialize: function() { console.log(this);}
    });
    

    OR

     var myObject = Class({
            initialize: function() { console.log(createdObject);}
        });
    

    OR

    var myObject = Class({
                initialize: function() { var self = this; console.log(self );}
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a class that will represent two different behaivours at
I'm trying to make spreadsheet with class/method modifiers. The spreadsheet itself can be located
I was trying to make my own class for currencies using longs, but apparently
I'm trying to make a simple TCP server using Twisted ,which can do some
I am trying to make a factory that will have the type passed in,
I am trying to make two testcases using class org.springframework.ws.client.core.WebServiceTemplate . Both testcases are
I'm trying to make a multithread program that acts as a furniture factory... For
I'm trying to make a class where I put a key and value into
I´m trying to make a custom component to javafx, so I make my class
I've been trying to make a generic class to represent a range of values

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.