JavaScript碰到的几个方法

=>isNaN() 函数用于检查其参数|是否|是|非数字值

绕吧,我给它断个句,别一不小心看叉了

百度百科告诉我们,NaN,是Not a Number的缩写

所以,

alert(isNaN(123));==>flase
alert(isNaN(-1.23));==>false
alert(isNaN(5-2));==>false
alert(isNaN(0));==>flase
alert(isNaN("Hello"));==>true
alert(isNaN("2005/12/12"));==>true

=>Number() 函数把对象的值转换为数字

用文言文来解释就是使役动词,哦不,这里要叫使役函数了,使对象成为数字

如果参数是 Date 对象,Number() 返回从 1970 年 1 月 1 日至今的毫秒数。

如果对象的值无法转换为数字,那么 Number() 函数返回 NaN。

w3school的例子能更好地理解

<script type="text/javascript">
  var test1= new Boolean(true);
  var test2= new Boolean(false);
  var test3= new Date();
  var test4= new String("999");
  var test5= new String("999 888");

  document.write(Number(test1)+ "<br />");//1
  document.write(Number(test2)+ "<br />");//0
  document.write(Number(test3)+ "<br />");//1256657776588
  document.write(Number(test4)+ "<br />");//999
  document.write(Number(test5)+ "<br />");//NaN
</script>

随机值
Math.floor ( Math.random ( ) * n + 1 ),表示生成1-n的随机数
Math.floor ( Math.random ( ) * n ),表示生成0-(n-1)的随机数
Math.floor ( Math.random ( ) * n + a ),表示生成a-(n-1+a)的随机数,或者说,是从a在内的之后n个数

原文地址:https://www.cnblogs.com/yumeixin/p/5015710.html