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

  • Home
  • SEARCH
  • 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 8941879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:24:03+00:00 2026-06-15T11:24:03+00:00

I have a constructor object. Inside this constructor i have two functions. One is

  • 0

I have a constructor object. Inside this constructor i have two functions. One is called window.onresize and the other one is onBrowserResize. Now i want to call onBrowserResize inside the window.onresize function. How do i do this?

function MyObject() {
    window.onresize = function(event) {
        this.onBrowserResize();
    }

    this.onBrowserResize = function() {
    }
}

I would like to have the function public so i can call it like this: object.onBrowserResize();

If I make the function private like this: var onBrowserResize = function() then I cannot access it outside the scope.

Anyone has an idea how to do it? I’m probably just a bit confused about the scoping in objects.

  • 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-15T11:24:04+00:00Added an answer on June 15, 2026 at 11:24 am

    In JavaScript, the value of this is determined depending on how the function is called. See the MDN article on this for details.

    Your question is missing some details. My answer is assuming you have something like this:

    var MyObj = function() {
        window.onresize = function(event) {
            this.onBrowserResize();
        }
    
        this.onBrowserResize = function() {}
    }
    

    Which is instanced like this:

    var myObj = new MyObj();
    

    Here are some of ways to achieve that.

    1. Just passing this.onBrowserResize as the event handler (the simplest way to go, if all your onresize handler does is call onBrowserResize):

    var MyObj = function() {
        window.onresize = this.onBrowserResize;
        this.onBrowserResize = function() {
            // caution: "this" here won't be your object.
            // use option 3 below if you need this to be your object
        }
    }
    

    2. Caching a reference to the local this:

    var MyObj = function() {
        var that = this;
        window.onresize = function(event) {
            that.onBrowserResize();
        }
    
        this.onBrowserResize = function() {}
    }
    

    3. Fixing the value of this with Function.prototype.bind:

    var MyObj = function() {
        window.onresize = this.onBrowserResize.bind(this);
    
        this.onBrowserResize = function() {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
I have a ManagedBean say clsA, inside this clsA has a BOC object that
I have two classes under QT, one to make a form, the other to
In the constructor of an object I have the following code: ConfigReader::ConfigReader(){ TiXmlDocument doc(CONFIGURATION_FILE_LOCATION);
I have a class constructor that expects a reference to another class object to
I have an object type with no-args constructor, but when I specify it as
I have a Uri object being passed to a constructor of my class. I
I have a mocked object that is passed as a constructor argument to another
I have an object to be scoped as a prototype, and the constructor argument
Hi I have a question about this pointer, when an object is constructed, when

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.