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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:12:00+00:00 2026-06-07T06:12:00+00:00

I’m a newbie to javascript and I need some help. I was trying to

  • 0

I’m a newbie to javascript and I need some help.
I was trying to sum radius by function, but got an undefined error:(

function sumWithFunction(func, number) {
    return func() + number;
}

function Circle(X, Y, R) {
    this.x = X;
    this.y = Y;
    this.r = R;
}
Circle.prototype.getRadius = function () {
    return this.r;
}
Circle.prototype.increaseRadiusBy = function(number) {
    this.r = sumWithFunction(this.getRadius, number);
}

function addFivetoIt(func) {
    func(5);
}

var MyCircle = new Circle(0, 0, 10);
addFivetoIt(MyCircle.increaseRadiusBy);
  • 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-07T06:12:01+00:00Added an answer on June 7, 2026 at 6:12 am

    The problem is that you’re passing a function a reference to another function, and the passed function is therefore losing scope! Here’s the offending line:

    Circle.prototype.increaseRadiusBy = function(number) {
        this.r = sumWithFunction(this.getRadius, number);
    }
    

    JavaScript objects are in some ways simpler than they appear. When you added the getRadius method to the Circle prototype, you were not defining a class method like you would in classical OO. You were simply defining a named property of the prototype, and assigning a function to the value of that property. When you pass this.getRadius as an argument to a static function, like sumWithFunction, the context of this is lost. It executes with the this keyword bound to window, and since window has no r property, the browser throws an undefined error.

    Put another way, the statement this.getRadius() is actually saying “execute the function assigned to the getRadius property of this, and execute it in the context of this. Without calling the function explicitly through that statement, the context is not assigned.

    A common solution to this is to add an expected argument to any function which receives another function, for context.

    function sumWithFunction(func, context, number) {
        return func.apply(context) + number;
    }
    
    function Circle(X, Y, R) {
        this.x = X;
        this.y = Y;
        this.r = R;
    }
    Circle.prototype.getRadius = function () {
        return this.r;
    }
    Circle.prototype.increaseRadiusBy = function(number) {
        this.r = sumWithFunction(this.getRadius, this, number);
    }
    
    function addFivetoIt(func, context) {
        func.apply(context,[5]);
    }
    
    var MyCircle = new Circle(0, 0, 10);
    addFivetoIt(MyCircle.increaseRadiusBy, myCircle);
    

    A simpler, but less robust solution would be to declare a function inline that can access a context reference in the local closure.

    function sumWithFunction(func, number) {
        return func() + number;
    }
    
    function Circle(X, Y, R) {
        this.x = X;
        this.y = Y;
        this.r = R;
    }
    Circle.prototype.getRadius = function () {
        return this.r;
    }
    Circle.prototype.increaseRadiusBy = function(number) {
        var me = this;
        this.r = sumWithFunction(function() {
            return me.getRadius()
        }, number);
    }
    
    function addFivetoIt(func) {
        func(5);
    }
    
    var MyCircle = new Circle(0, 0, 10);
    addFivetoIt(function(number) {
        return MyCircle.increaseRadiusBy(number);
    });
    

    But by far the simplest solution is to use a newer feature of ECMAScript, a function method called bind. It is explained well here, including the fact that it is not supported by all browsers. That’s why a lot of libraries, like jQuery, Prototype, etc., have cross-browser function-binding utility methods like $.proxy.

    function sumWithFunction(func, number) {
        return func() + number;
    }
    
    function Circle(X, Y, R) {
        this.x = X;
        this.y = Y;
        this.r = R;
    }
    Circle.prototype.getRadius = function () {
        return this.r;
    }
    Circle.prototype.increaseRadiusBy = function(number) {
        this.r = sumWithFunction(this.getRadius.bind(this), number); // or $.proxy(this.getRadius,this)
    }
    
    function addFivetoIt(func) {
        func(5);
    }
    
    var MyCircle = new Circle(0, 0, 10);
    addFivetoIt(MyCircle.increaseRadiusBy.bind(MyCircle)); // or $.proxy(MyCircle.increaseRadiusBy,MyCircle)
    
    • 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
I am trying to render a haml file in a javascript response like so:
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which 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.