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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:33:20+00:00 2026-05-22T14:33:20+00:00

I’m at a total loss. I have a function.. Number.prototype.abs = function () {

  • 0

I’m at a total loss.

I have a function..

Number.prototype.abs = function () {
    return this >= 0 ? this : this * -1;
};

..that returns the absolute value of a number..

(50).abs(); // 50
(-50).abs(); // 50

..but that doesn’t compare correctly..

(50).abs() === 50; // False

..sometimes.

(50).abs() == 50; // True
(-50).abs() === 50; // True

The thing about it, is that it works in Chrome 12, and Firefox 4, but not in IE 9, Safari 5, or Opera 11.

I don’t see anything wrong with the code, and since it works in Chrome and Firefox, it’s something browser specific but I don’t know what.

Update: The browser specific difference is strict mode support. I run my code in strict mode, which introduces some changes that make my code work. The reason it failed in the browsers it did, is because they have an incomplete or missing strict mode.

Why is it returning false?

  • 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-22T14:33:21+00:00Added an answer on May 22, 2026 at 2:33 pm

    Even though Jeremy Heiler is correct, his justification is misconstrued. Why you’re getting object instead of number has nothing to do with constructors.

    The problem here is the this keyword. You need to understand what happens whenever you use this. A little bit of digging through the ECMA draft will show you that

    The this keyword evaluates to the value of the ThisBinding of the current execution context.

    (I would change the above. The this keyword doesn’t evaluate to the value of anything as we’ll soon see.) Hmm, okay, but how exactly does ThisBinding work? Read on!

    The following steps are performed when
    control enters the execution context
    for function code contained in
    function object F, a caller provided
    thisArg, and a caller provided
    argumentsList:

    1. If the function code is strict code, set the ThisBinding to thisArg.
    2. Else if thisArg is null or undefined, set the ThisBinding to the
      global object.
    3. Else if Type(thisArg) is not Object, set the ThisBinding to
      ToObject(thisArg).
    4. Else set the ThisBinding to thisArg.
    5. Let localEnv be the result of calling NewDeclarativeEnvironment
      passing the value of the [[Scope]]
      internal property of F as the
      argument.
    6. Set the LexicalEnvironment to localEnv.
    7. Set the VariableEnvironment to localEnv.
    8. Let code be the value of F’s [[Code]] internal property.
    9. Perform Declaration Binding Instantiation using the function code
      code and argumentList as described in 10.5

    And therein lies the rub (look at the bolded part). If a function is ever called from a non-object context, ThisBinding (aka using this) always returns the value of the context wrapped inside an object. The easiest way to fix it would be to do:

    Number.prototype.abs = function () {
        "use strict"; // if available
        // * 1 implicitly coerces 'this' to a number value
        return this >= 0 ? this * 1 : this * -1;
        //... or Number(this) explicitly coerces 'this' to its toNumber value
        return Number(this >= 0 ? this : this * -1);
    };
    

    …to coerce the this object (or to force strict mode). But I think it’s important to understand how this works, and this question is a great example of that.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
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
I have some data like this: 1 2 3 4 5 9 2 6
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
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.