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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:30:49+00:00 2026-05-30T14:30:49+00:00

I am trying to create a JavaScript object as follows. var MyObject = function

  • 0

I am trying to create a JavaScript object as follows.

var MyObject = function (parameters) {
    this.parameters = parameters;
    parameters.userFunction(this.MyObjectCallback);
}

MyObject.SOME_STATIC_VARIABLE = 21;

MyObject.prototype = {
    myObjectCallback: function() {
         console.log(this);
    }
}

The MyObject object will accept a userFunction to which it will pass a handler. The user function will do some logic and pass the result back to the instance, for example:

new MyObject({userFunction: function(callback) {
   $.post(
        'http://localhost/~knyttl/source.php',
        {},
        callback,
        'json');,
}});

Unfortunately, even though the callback is properly called, this gets an instance of the JQuery object and not of the MyObject instance as I would like. To conclude, I can not manage to keep the MyObject instance.

I am not even sure, whether this is a correct way of creating JavaScript objects. I will be grateful for any suggestion.

  • 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-30T14:30:50+00:00Added an answer on May 30, 2026 at 2:30 pm

    You can bind a specific this value using .bind. Also I corrected the capitalizing of My.

    parameters.userFunction(this.myObjectCallback.bind(this));
    

    When you call a function like a.b(), then inside b, this === a. However, if you do not directly call it but only pass the function (like a.b) and call it later, this binding is lost.

    .bind returns a new function which now receives the jQuery ajax result as this. However, it ignores that and calls myObjectCallback with the predefined (bound) this.

    .bind is not available on older browsers but there are shims available.

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

Sidebar

Related Questions

SO, I'm trying to create a javascript object, and use the setInterval method. This
I'm basically trying to create a generic object in javascript that I can use
I am trying to create a JavaScript object that will deal with all of
Trying to create an object in Javascript (for Appcelerator/Titanium). The object is defined like
So i am trying to learn object oriented programming in javascript. function doStock() {
I am trying to create a reusable javascript object (I want to use new).
I am new to javascript... I trying to create an object- Flower. Every Flower
I am trying to create some javascript that when an object is added to
I am trying to create an object in javascript that has an animation run
Okay, I am trying to create an object in JavaScript. The project is a

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.