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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:42:05+00:00 2026-05-22T00:42:05+00:00

I’m trying to make a function that can be applied to a value returned

  • 0

I’m trying to make a function that can be applied to a value returned from another function both within a function. Since that’s probably a terrible explanation, here’s a simplified sample:

function MainFnc() {
    this.subFnc=function(a) {
        return a*2
    }
    this.subFnc.subSubFnc=function(a) {
        return this*a
    }
}

This isn’t my actual code – it’s for a far better reason than multiplying numbers. This is just a simplified example of what I’m trying to achieve. My question is whether or not it’s actually possible to go this deep, and if so how? The method I’ve portrayed in this sample code evidently does not work.

Thanks for any help.

Edit: Here’s an example of it in use since not everyone understands clearly what I want to do with this:

anObject=new MainFnc;
alert(anObject.subFnc(2)); //returns 4
alert(anObject.subFnc(2).subSubFnc(2); //returns 8

This is not exactly what I’m doing, it’s just easier to understand using simple multiplication.

  • 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-22T00:42:06+00:00Added an answer on May 22, 2026 at 12:42 am

    Update based on your comment:

    MainFnc is an object which is created in a variable (ie MainVar). So if I wanted to try MainVar.subFnc(2) it’d return 4. If I wanted to try MainVar.subFnc(2).subSubFnc(2), however, it’d return 8.

    Right now, you’re returning a number from your subFnc, and so the expression MainVar.subFnc(2).subSubFnc(2) breaks down like this:

    1. Look up the property subFnc on MainVar; it returns a function reference.
    2. Call the function with this = MainVar; this returns the number 2.
    3. Look up the property subSubFnc on the number 2; it returns undefined.
    4. Call the function with this = 2; fails because you can’t call undefined as a function.

    More: You must remember this and Mythical Methods

    To do what you’re doing, you’d have to have subFnc return an object with function properties. You could do it like this:

    function MainFnc(val) {
        this.value = val;
    
        this.mult=function(a) {
            return new MainFnc(this.value * a);
        };
    
        this.div=function(a) {
            return new MainFnc(this.value / a);
        };
    
        this.get = function() {
            return this.value;
        };
    }
    

    …and then call it like this:

    var MainVar = new MainFnc(3);
    alert(MainVar.mult(3).mult(4).div(6).get()); // alerts "6" (3 * 3 * 4 / 6 = 6)
    

    Live example

    Note the get function to return the underlying number. You might also add a toString:

    this.toString = function() {
        return String(this.value);
    };
    

    But the above isn’t taking advantage of prototypical inheritance at all (and it will be important to, if you’re creating all of those objects; we need to keep them lightweight); you might consider:

    function MainFnc(val) {
        this.value = val;
    }
    MainFnc.prototype.mult = function(a) {
        return new MainFnc(this.value * a);
    };
    MainFnc.prototype.div = function(a) {
        return new MainFnc(this.value / a);
    };
    MainFnc.prototype.get = function() {
        return this.value;
    };
    MainFnc.prototype.toString = function() {
        return String(this.value);
    };
    

    Original Answer:

    With that code, if you did this:

    var f = new MainFnc();
    alert(f.subFnc(3));           // alerts "6"
    alert(f.subFnc.subSubFnc(3)); // NaN
    

    …because this inside subSubFnc when called like that is subFnc, and multipling a function reference tries to convert it to a number, which comes out NaN, and so the result of the multiplication is NaN.

    Remember that in JavaScript, this is defined entirely by how a function is called, not where the function is defined. When you call a function via dotted notation (a.b();), the object you’re looking up the property on becomes this within the function call, and so with a.b.c();, this within c() is b, not a. More: You must remember this and Mythical Methods

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

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.