Chrome/Chromium doesn't know JavaScript function Math.sign

Michael

For a scrolling feature I use some Math-functions:

transform: "rotateY("+(Math.sign(this._backlog)*Math.sqrt(Math.abs(this._backlog)))+"deg)"

While this works well in Firefox, it doesn't work in Chrome with following message:

Uncaught TypeError: Object #<Object> has no method 'sign' 

Math.abs and Math.sqrt are working.

Which function I can use in Chrome?

Dave

Math.sign is only part of the draft specification for ES6 (§20.2.2.28), which is incomplete. Support for as-yet-unspecified features is likely to be spotty from engine to engine.

MDN previously claimed that Chrome 32 supported this, but as far as I can tell that was simply wrong. My version of Chrome (36) does not support it, and MDN now claims that only FireFox supports this function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign.

But this is a pretty easy function to write yourself:

function sign(x){
    if( +x === x ) { // check if a number was given
        return (x === 0) ? x : (x > 0) ? 1 : -1;
    }
    return NaN;
}

NaN, +/-Infinity and -0 are handled correctly (sign(-0)==-0, sign(NaN)==NaN), and non-numeric inputs will return NaN. If you don't care about non-numeric inputs you can use this simplified one-liner (which still handles NaN, +/-Infinity and -0 but does not check the input type):

function sign(x){return x>0?1:x<0?-1:x;}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Chrome/Chromium doesn't know JavaScript function Math.sign

From Dev

Javascript Function doesn't be called

From Dev

Javascript function doesn't work?

From Dev

javascript function doesn't responding

From Dev

Splint doesn't know that library function is freeing memory

From Dev

What does the operator/math sign ">>" mean in Javascript?

From Dev

$ sign in JavaScript function

From Dev

Javascript math function resolver

From Dev

Using math.h for sin function and getting errors don't know why

From Dev

Devise doesn't sign in after sign up

From Dev

Javascript doesn't run the whole function (JSLInt doesn't mention)

From Dev

I don't know how to implement this function in javascript

From Dev

JavaScript onClick function doesn't work

From Dev

Javascript reduce function doesn't work on this obj

From Dev

Javascript function doesn't break on return?

From Dev

why this javascript decorate function doesn't work?

From Dev

Javascript doesn't work after call a function

From Dev

JavaScript function doesn't execute everything

From Dev

Return doesn't exit recursive function in javascript

From Dev

Chrome doesn't declare my JavaScript function

From Dev

my anonymous function in javascript doesn't work

From Dev

Javascript function result doesn't show in firefox

From Dev

html onsubmit doesn't start Javascript function

From Dev

Javascript function doesn't return anything

From Dev

Javascript Closures with doesn't work with returned function

From Dev

Calling my Javascript function doesn't work

From Dev

javascript function doesn't get called

From Dev

JavaScript split function doesn't work

From Dev

Why function Javascript doesn't execute?

Related Related

HotTag

Archive