[英]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)==-0
, sign(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.