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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:26:23+00:00 2026-05-11T07:26:23+00:00

I have the following: var o = {f: function(fn) { fn.call(o); }}; var ob

  • 0

I have the following:

var o = {f: function(fn) {     fn.call(o); }}; var ob = {f: function() {     o.f(function() {         this.x = 2; //HERE: how can this reference ob?         //ob.x = 2;     }); }}; ob.f(); ob.x; // undefined 

o.f(fn) calls fn where this is bound to o.

At HERE, I want to use this to access ob. However, when ob.f is called, this is bound to o. JQuery works like this, I think. For example:

$(...).blah(function() {     this // this is bound to $(...) jquery object.     ... }; 

What I’m doing now is:

var Ob = function() {     var self = this;     self.f = function() {         o.f(function() { self.x = 2; };     }; }; var ob = new Ob(); ob.f(); ob.x; // 2 

But I don’t like above for stylistic reasons:

  1. Using the new operator sounds like too classical OOP.
  2. Defining class Ob using function isn’t intuitive (at least in the beginning).

That’s why I am trying to define ob with an object literal. But I can’t find a way to reference the object ob in a function that uses method call that sets this to other object than ob.

I can do something like the following:

var ob = {f: function() {     o.f(function() {         self.x = 2;     }); }}; var self = ob; ob.f(); ob.x; 

But I don’t know how to factor above. I tried:

function obj(o) {     return function() {         var self = o;         return o;     }(); } var ob = obj({f: function() {     o.f(function() {         self.x = 2;     }); }}); ob.f(); ob.x;// ReferenceError: self is not defined 

So, is there a way to reference the object in a function inside the object reliably (this can bound to anything depending on the context)?

  • 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. 2026-05-11T07:26:24+00:00Added an answer on May 11, 2026 at 7:26 am

    Following Douglas Crockfords simple constructor pattern, I would make a constructor-function that uses the object literal instead of new. Like this:

    var o = {f: function(fn) {     fn.call(o); }};  function obj() {     var me = {};     me.f = function () {         o.f(function() {             me.x = 2;         });     };     return me; }  var ob = obj(); ob.f(); ob.x; // 2 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: this.alphaArray = function() { var alpha = this.alphStr, ,
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following snippet of JS var Customer : function() { this.ShipProduct :
I have the following jQuery ajax call setup: function Testing() { var result =
I have the following: var doHandler = function (link, form, close) { var $form
I have the following code: var submitHandler = function ($link, $form, close) { var
I have the following code: var refreshId = setInterval(function() { $(#footad).html('myjshere'); }, 15500); Where
I have the following: $(document).ready(function() { var myIframe = document.getElementById('myIframe'); var element = myIframe.contentDocument.createElement('script');
I have the following structure $('foo').click( function() { var bool = false; $('bar').animate( function()
I have the following code: $(document).ready(function() { var id = 'cbx'; var idPrefix =

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.