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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:58:18+00:00 2026-06-14T19:58:18+00:00

I’ve created the following object: function Calculator() { //some code here, including object variables

  • 0

I’ve created the following object:

function Calculator() { 
    //some code here, including object variables and functions, such as:
    this.add = function(x) {
       //again, irrelevant code
    }
    //similar methods
}


var calc = new Calculator();

And then, I tried to do the following:

var met = calc.add;
met(5);

But it didn’t work.

(I’ve checked everything – the variable ‘met’ is of type ‘function’, when I “alert” it to be certain, it alerts the proper string – function(x){...}, etc. But calling met(7) does nothing, while calling calc.add(7) adds the number)

Does anyone know why, or how can I fix it? (Can I fix it?)

  • 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-14T19:58:19+00:00Added an answer on June 14, 2026 at 7:58 pm

    Actually, the function inside add is not irrelevant. The problem you’re having is because how this behaves in javascript.

    When you call it like this:

    met(5);
    

    it’s the same as doing it like this:

    window.met(5); // assuming browsers
    

    Which means that the this in met is bound to the global object. So the reason it doesn’t work is because whatever variable you’re trying to add to doesn’t exist in the global object. You can test it simply by declaring that variable in the global object (which for browsers happen to be global variables):

    var foo = {
        i = 0,
        incr = function(){ return ++this.i }
    }
    foo.incr(); // this works as expected
    
    // now assign foo.incr to the global object:
    var inc = foo.incr;
    inc(); // this fails because window.i doesn't exist
    
    // create i in window
    i =0;
    inc(); // now this works, maybe a bit unexpected
    

    If you want to simply alias add to met but still operate on the calc object you have two options. The first is to make sure that add is called as a method of calc:

    var met = function(x) { return calc.add(x) };
    met(5);
    

    This is simple and it works as expected. By adding an anonymous function wrapper we can call add as calc.add() which makes this bound to calc.

    The second is as mentioned by @Guffa: use call or apply to point this to whatever you want:

    var met = calc.add;
    met.call(calc,5);
    

    To understand more about how this works in javascript read this: How does the "this" keyword in Javascript act within an object literal?

    • 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 have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
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
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.