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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:18:52+00:00 2026-06-15T08:18:52+00:00

I am trying to understand the way that javascript passes functions around and am

  • 0

I am trying to understand the way that javascript passes functions around and am having a bit of a problem groking why a prototype function can NOT access a var defined in a function constructor while a function defined in the constructor can access the var. Here is code that works:

   var model = function model() {
       this.state = 1;
       this.GetState =   (function(scope){
           return function(){ return scope.state;};
       })(this);
   }

    var othermodel = function othermodel(mdl) {
        this.GetStateFn = mdl.GetState;
    }

    othermodel.prototype.WriteState = function() {
        console.log(this.GetStateFn.call());
    };

    var m = new model();
    var o = new othermodel(m)


    o.WriteState();

This works and makes sense – the GetState() function can access this.state.

However, if I create GetState as follows:

 model.prototype.GetState =   (function(scope){
        return function(){ return scope.state;};
    })(this);

The result will be an error that scope is not defined.

I would prefer to have this work with the prototype method as I do not want a copy of the function in ever model, but it would seem that prototype can’t work because it can’t access the specific instance of the model.

So, can someone provide me with a good explanation of a) what I need to do to get this to work with prototype (assuming I can) and b) if I can’t get it to work with prototype, what is the reason so I can understand better the underpinnings of the issue.

  • 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-15T08:18:53+00:00Added an answer on June 15, 2026 at 8:18 am

    Why not simply write the function this way

    model.prototype.GetState = function() { return this.state; }
    
    var othermodel = function othermodel(mdl) {
        this.GetStateFn = mdl.GetState.bind(mdl);
    }
    
    othermodel.prototype.WriteState = function() {
        console.log(this.GetStateFn.call());
    };
    

    The above code will work, as in most cases you will execute code like m.GetState(). That is an example of invoking a function as an object method. In that case, this is guaranteed to point to the object m. You seem to know how the prototype chains work, so I won’t go there.

    When assigning the function reference to the other model, we use the .bind to ensure that within GetState, this points to mdl. Reference for bind: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind

    Your original IIFE’s were in effect your implementation of bind. The issue was the value of this was wrong. Currently, every time you need to assign models function to some other function, you will need to use bind at all those times. You have tagged your question as node.js, bind is available on the Function prototype in node.js and any ES5 compatible browser. If you need to run the above code on older browsers or environments that do not support bind, replace bind with your IIFE.

    As for why your code isn’t working,

    model.prototype.GetState =   (function(scope){
        return function(){ return scope.state;};
    })(this);
    

    Here, this doesn’t refer to the eventual model object (m). this can refer to any one of 5 options in javascript. Refer: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this

    Lets assume the above code is in an html file inside some script tag. Then this will refer to the window object. window doesn’t have any property called state, hence the undefined. If you were to console.log(this.m, this.o) at the end of you script, you would see the respective m and o objects.

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

Sidebar

Related Questions

I'm trying to understand pure prototype-based JavaScript and one specific thing I'm struggling with
I'm trying to understand inheritance in JavaScript. I'm using prototype to link a child
I'm trying to understand the syntax of how to put together a JavaScript function
I have been trying to understand the way ActionScript's events are implemented, but I'm
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
I am trying to understand DbConnection and DbCommand, and the proper way to dispose
Trying to understand Ruby a bit better, I ran into this code surfing the
I am trying to understand how to develop stand-alone Javascript code. I want to
I am having problems trying to use the regular expression that I used in
I am trying to understand conceptually the best way to deliver real streaming audio

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.