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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:20:29+00:00 2026-05-20T03:20:29+00:00

i have JavaScript components, that has following architecture: var MyComponent = function(params) { setup(params);

  • 0

i have JavaScript components, that has following architecture:

var MyComponent = function(params)
{
    setup(params);


    this.doSomething()
    {
        // doing something
    };

    function setup(params)
    {
        // Setup

        // Interaction logic
        var _this = this; // "this" points to DOMWindow, not to created object
        $(".some-element").click(function(){
            _this.doSomething(); // it craches here, because of above
        });
    }
};

When something, being controlled by interaction logic, happens, sometimes i must forward execution to “public” methods of component.

In this situation, i have a problem with “this” pointer.

Sample code demonstrates it:

var Item = function()
{
    this.say = function()
    {
        alert("hello");
    };
    this.sayInternal = function()
    {
        _sayInternal();
    };
    function _sayInternal()
    {
        this.say();
    };
};

To test it,

  • Create an object:

var o = new Item();

  • This works fine:

o.say(); // alerts "hello"

  • This crashes:

o.sayInternal();

I get an error:

TypeError: Result of expression ‘this.say’ [undefined] is not a function.

I think, such a behaviour takes place, because _sayInternal() function is declared (and not assigned to object, like “this.say = function()”). This way, it is shared across all created objects and acts like a static function in C++.

Is this true ?

  • 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-20T03:20:29+00:00Added an answer on May 20, 2026 at 3:20 am

    No, sayInternal is not shared between created objects. But you are right, the created objects don’t have access to sayInternal as it is not assigned to them. This function is only local to the constructor function.

    this always refers to the context a function is invoked in. If you call it like func(), then this refers to the global object (which is window in browser). If you set the function as property of an object and call it with obj.func(), then this will refer to obj.

    If you assign a “bound” function to a variable and call it:

    var method = obj.func;
    method();
    

    then this will again refer to the global object. In JavaScript, functions are like any other value, they don’t have a special relationship to the object they are assigned to.


    You can explicitly set the context with call or apply:

    var MyComponent = function(params)
    {
        setup.call(this, params); // <- using `call`
    
        this.doSomething()
        {
            // doing something
        };
    
        function setup(params)
        {
            // Setup  
            // Interaction logic
            var _this = this; // "this" to new  created object
            $(".some-element").click(function(){
                _this.doSomething();
            });
        }
    };
    

    or in you other example:

    var Item = function()
    {
        this.say = function()
        {
            alert("hello");
        };
        this.sayInternal = function()
        {
            _sayInternal.call(this);
        };
        function _sayInternal()
        {
            this.say();
        };
    };
    

    That said, this approach to assign functions to objects is not good, because every instance will have its own this.sayInternal function. So for the Item code above, every creation of an instance involves creating three functions too, which is a waste of memory.

    Making use of prototype inheritance would be a better way:

    var Item = function() {
    };
    
    Item.prototype = (function() {
        function _sayInternal() {
            this.say();
        };
    
        return {
            say: function() {
                alert("hello");
            },
            sayInternal: function(){
                _sayInternal.call(this);
            }
        }
    }());
    

    This way, _sayInternal is only created once and all instances inherit (refer to) the prototype, so say and sayInternal also exist only once. The “trick” with the immediate function makes _sayInternal only accessible by say and sayInternal.

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

Sidebar

Related Questions

I have a javascript function that manipulates the DOM when it is called (adds
I have a JavaScript resource that has the possibility of being edited at any
I have a JavaScript function, pop_item . I have to call this from PHP,
I have a component which writes/generates javascript from a server side renderer. This component
I have a JavaScript method that I need to run on one of my
We have a JavaScript function named move which does just windows.location.href = any given
I have a JavaScript snippet that runs very well on Firefox and Safari, but
I have a JavaScript class that I have made and put it into its
I have a javascript routine that is performing actions on a group of checkboxes,
I have a JavaScript widget which provides standard extension points. One of them is

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.