简体   繁体   English

Chrome / Chromium不知道JavaScript函数Math.sign

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

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: 尽管此方法在Firefox中运行良好,但在Chrome中无法显示以下消息:

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

Math.abs and Math.sqrt are working. Math.abs和Math.sqrt正在运行。

Which function I can use in Chrome? 我可以在Chrome中使用哪些功能?

Math.sign is only part of the draft specification for ES6 (§20.2.2.28), which is incomplete. Math.sign只是ES6 规范草案 (第20.2.2.28节)的一部分,该草案不完整。 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. MDN先前声称Chrome 32支持此功能,但据我所知这完全是错误的。 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 . 我的Chrome(36)版本不支持它,并且MDN现在声称只有FireFox支持此功能: https : //developer.mozilla.org/zh-CN/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. NaN,+ /-Infinity和-0可以正确处理( sign(-0)==-0sign(NaN)==NaN ),并且非数字输入将返回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): 如果您不关心非数字输入,则可以使用以下简化的单行格式(它仍然可以处理NaN,+ /-Infinity和-0,但不检查输入类型):

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

一线功能:

var sign = function(n) {return n>0?1:n=0?0:-1;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Math.sign([]) = 0,Math.sign([20]) = 1,Math.sign([20, 30, 40]) = NaN? - Why is Math.sign([]) = 0, Math.sign([20]) = 1, and Math.sign([20, 30, 40]) = NaN? 有什么方法可以比较负 0 即。 -0 这是从 Math.sign(-0) 返回的? - Is there any way to compare negative 0 ie. -0 which is returned from Math.sign(-0)? Chrome上的Math.random()javascript函数* undefined * - Math.random() javascript function *undefined* on Chrome javascript TypeError:Math.random 不是 chrome 上的函数 - javascript TypeError: Math.random is not a function on chrome Math.round在JavaScript中不起作用 - Math.round doesn't work in JavaScript 我的“ changeColor” JavaScript函数无法在Google Chrome浏览器上使用 - My “changeColor” javascript function doesn't work on Google Chrome JavaScript退出弹出功能在Chrome和IE中无法正常工作 - JavaScript exit popup function doesn't work properly in Chrome and IE Raspberry Pi-JavaScript在Chrome Kiosk模式下不起作用 - Raspberry Pi - JavaScript doesn't work in chromium kiosk mode localStorage(需要帮助不正确 function) - Javascript Chrome 扩展 - localStorage (Need help doesn't function properly) - Javascript Chrome Extension 是否存在这样的情况:Javascript 中的 Math.ceil function 不会从 output 中删除小数? - Is there a condition where the Math.ceil function in Javascript doesn't remove decimals from the output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM