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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:53:21+00:00 2026-05-25T22:53:21+00:00

Consider this: function f2(x) { return x+1; }; X = function(){ this.f1=function (x) {

  • 0

Consider this:

function f2(x) {
    return x+1;
};

X = function(){
    this.f1=function (x) {
      return 2*f2(x);
    }

    return this;
};

then x = new X(); x.f1(1) works fine.

But when i want to do this:

X = function(){
    this.f2 = function(x) {
        return x+1;
    };

    this.f1=function (x) {
        return 2*f2(x);
    }

    return this;
};

The same statement will complain that it can’t find f2.
In, for example c#, you can say

class X {
   int f2(int x){return x+1;}
   int f1(int x){return 2*f2(x);}
}

and this will work

X x=new X();
x.f1(1)

Why?

  • 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-25T22:53:21+00:00Added an answer on May 25, 2026 at 10:53 pm

    To reference f2 in your second code block, you’ll need to use this.f2. this references the context in which the function is being executed. Since you call f1 in the following way:

    x.f1();
    

    … the context is set to the instance, x.

    JavaScript does not make instance variables available to the scope in the same way as scope variables, i.e. those directly available:

    X = function(){
    
        var f2 = 123;
    
        this.f2 = function(x) {
            return x+1;
        };
    
        this.f1=function (x) {
    
            console.log(f2); // => 123
            console.log(this.f2); // => function(){}
    
            return 2 * this.f2(x);
        };
    
        return this;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
Consider: function Shape() { this.name = "Generic"; this.draw = function() { return "Drawing "
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider this code: var img = new Image(); img.onload = function() { console.log(this.width); };
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Before Consider to have a class and a global function: This is, for example,
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider the following example where Student inherits from Person : function Person(name) { this.name

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.