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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:13:42+00:00 2026-06-03T03:13:42+00:00

I’m trying something like this: (function() { var Foo, foo; Foo = function(proto) {

  • 0

I’m trying something like this:

(function() {
  var Foo, foo;

  Foo = function(proto) {
    var obj, privateMethod, publicMethod;
    privateMethod = function() {
      return console.log("private", this);
    };
    publicMethod = function() {
      console.log("public", this);
      return privateMethod();
    };
    obj = Object.create(proto);
    obj.bar = publicMethod;
    return obj;
  };

  foo = new Foo({
    baz: "dooz"
  });

  foo.bar();

}).call(this);

Obviously this is the object itself when publicMethod is called, but is set to the global object in the privateMethod. I know it can be solved by changing:

      return privateMethod();

to:

      return privateMethod.call(this);

I know that this get’s lost when a function is nested in a function, but did not expect that case here. Do I encounter this JavaScript bug here or is there something that I simply do not understand yet?

  • 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-03T03:13:43+00:00Added an answer on June 3, 2026 at 3:13 am

    Context (this), in javascript, is set by how a function is called, and is in no way a property of the function itself.

    obj.bar = function() { console.log(this) };
    obj.bar()    // obj
    obj['bar']() // obj
    
    // But break the function off the object, and this changes
    fn = obj.bar
    fn() // window
    

    What this example shows us is that the the dot syntax there sets this. Think of obj.bar() is syntax sugar for obj.bar.call(obj).

    So your public method gets the right this, because of how it’s called in external code.

    foo.bar();
    

    But your private method is invoked with no receiver at all.

    return privateMethod();
    

    So no context is assigned, and it defaults to the global object.


    So given you are creating these functions in the constructor, you have some flexibility.

    You could assign the proper value of this in the constructor to something else, and use that in your private functions. (Likely the best option)

    var self = this;
    privateMethod = function() {
      return console.log("private", self);
    };
    

    Or if your JS target engine support Function.prototype.bind (not all do, sadly) you could do:

    privateMethod = function() {
      return console.log("private", this);
    }.bind(this);
    

    Which will return a function that has an assigned context, no matter what.

    Or you could bind it manually yourself.

    _privateMethod = function() {
      return console.log("private", this);
    }
    
    Foo = function(proto) {
      privateMethod = function() {
        _privateMethod.call(this);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.