标签:
内部类console.info( Math.ceil( 1.1 ) ); // 2 console.info( Math.ceil( 1.6 ) ); // 2 console.info( Math.floor( 1.1 ) ); // 1 console.info( Math.floor( 1.7 ) ); // 1 /* [intNum1, intNum2] 的整数 <==> Math.round( Math.random() * (intNum2 - intNum1) + intNum1 ) 0 < x < 1 3 <= y <= 100 0 * 97 < x * 97 < 1 * 97 ==> 0 < x * 97 < 97 ==> 0 + 3 < x * 97 + 3 < 97 + 3 ==> 3 < x * 97 + 3 < 100 ==> 3 <= Math.round( x * 97 + 3 ) <= 100 var x = Math.random() * (intNum2 - intNum1) + intNum1; x = Math.round( x ); */ for (var i = 0; i < 20; i++) { // [3, 100] var x = Math.random() * (100 - 3) + 3; x = Math.round( x ); console.info( x ); }
var date = new Date(); // 格林威治时间 // console.info( date ); // Date {Thu Nov 14 2013 12:40:42 GMT+0800} // 本地格式时间 // console.info( date.toLocaleString() ); // 2013年11月14日 12:42:04 var year = date.getFullYear(); var month = date.getMonth(); var dayOfMonth = date.getDate(); var dayOfWeek = date.getDay(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); console.info( "over..." ); /* 断点调试, 查看 year 2013 month 10 // 当前11月 dayOfMonth 14 dayOfWeek 4 hours 12 minutes 59 seconds 34 */
// ① str.indexOf(substr) 子串首次出现的索引 // ② str.split(separator,times) 分隔为字符串数组 // ③ str.substr(start,length) 截取指定个数, 支持负索引 // ④ str.substring(start,stop) 截取索引间字符, 包头不包尾 // ⑤ str.charAt(index) 返回指定位置的字符 // ⑥ toString() 内部函数都有 var str = "0123456789"; var length = str.length; var strArr1 = "1991-03-19".split("-"); var strArr2 = "1991-03-19".split(); var strArr3 = "1991".split(""); /* length 10 strArr1 ["1991", "03", "19"] strArr2 ["1991-03-19"] strArr3 ["1", "9", "9", "1"] */ str = "0123456789"; var subStr = str.substr(2, 4); var subStr2 = str.substring(2, 4); /* subStr "2345" subStr2 "23" */ str = "0123456789"; var ch = str.charAt(3); // ch "3" str = "a12b123c1234d" var index = str.indexOf("12"); var index2 = str.indexOf("12", 2); var index3 = str.indexOf("123455"); /* index 1 index2 4 index3 -1 */ console.info("over...");
//--- 数组的创建 // 动态添加数据 var arr = new Array(); arr[0] = "A"; arr[1] = "B"; arr["K"] = "kkk"; // length = 2, 但可引用 arr["K"] // 创建时初始化数据 var arr2 = new Array("A", "B"); // 初始化数组大小 var arr3 = new Array(2); // length = 2 // 注: 可动态扩充 /* ① arr.concat(arr1[,arr2,...]) 连接数组 ② arr.join(separator) 所有元素放入一个字符串,分隔符隔开 ③ arr.pop() 删除最后一个元素,并返回 ④ arr.push(val1[,val2,..]) 末尾添加元素,并返回新的长度。 ⑤ arr.reverse() 颠倒数组中元素的顺序 ⑥ arr.shift() 删除第一个元素,并返回 arr.unshift(var1[,...]) 往数组头部添加元素 ,并返回新的长度 ⑦ arr.sort([sortby]) 排序 */ arr = ["a", "b", "c", "d"]; arr2 = ["e", "f"]; //--- concat() toString() join() arr = arr.concat(arr2); // a,b,c,d,e,f console.info( arr.toString() ); console.info( arr.join( "-" ) ); // a-b-c-d-e-f //--- pop() console.info( arr.pop() ); // f console.info( arr.toString() ); // a,b,c,d,e arr = ["a", "b", "c", "d"]; //--- push() console.info( arr.push(1, 2) ); // 6 console.info( arr.toString() ); // a,b,c,d,1,2 //--- reverse() arr = ["a", "b", "c", "d"]; arr.reverse(); console.info( arr.toString() ); // d,c,b,a //--- shift() unshift() arr = ["a", "b", "c", "d"]; console.info( arr.shift() ); // a console.info( arr.toString() ); // b,c,d console.info( arr.unshift(1, 2) ); // 5 console.info( arr.toString() ); // 1,2,b,c,d //--- sort() 默认升序 arr = [2, 6, 3, 1, 4]; console.info( arr.sort() ); // [1, 2, 3, 4, 6] console.info( arr.toString() );//1,2,3,4,6 // 降序 arr = [5, 2, 6, 3, 1, 4]; arr.sort(function(x, y){ return y - x; }); console.info( arr.toString() ); // 6,5,4,3,2,1 console.info("over...");
var bool = new Boolean(true); console.info( bool.toString() ); console.info( bool.valueOf() ); console.info( typeof bool.valueOf() );
console.info( Number.MAX_VALUE ); // 1.7976931348623157e+308 var num = new Number(10); console.info( num.toString(2) ); // 1010 console.info( num.toString(3) ); // 101 console.info( num.toString(4) ); // 22 num = new Number(1.23456); console.info( num.toFixed(2) ); // 1.23 console.info( num.toFixed(3) ); // 1.235 console.info( num.toFixed(10) );// 1.2345600000
//--- NaN console.info( NaN ); console.info( parseInt("a") ); //--- undefined console.info( undefined ); var kk; console.info( kk ); /* encodeURI(URIstring) 把字符串编码为 URI。 decodeURI(URIstring) 解码某个编码的 URI。 eval(string) 计算某个字符串,并当成JS来执行。 isFinite() 检查某个值是否为有穷大的数。 isNaN() 检查某个值是否是数字。 parseFloat() 解析一个字符串并返回一个浮点数。 parseInt() 解析一个字符串并返回一个整数。 String() 把对象的值转换为字符串。 */ //--- encodeURI decodeURI var uri = "http://www.baidu.com?key=张"; var uriEncode = encodeURI(uri); var uriDecode = decodeURI(uriEncode); console.info(uri); console.info(uriEncode); console.info(uriDecode); /* http://www.baidu.com?key=张 http://www.baidu.com?key=%E5%BC%A0 http://www.baidu.com?key=张 */ //--- eval // eval("alert( '哇哈哈' )"); //--- isFinite console.info( 1/0 ); console.info( isFinite( 1/0 ) ); /* Infinity false */ //--- isNaN console.info( isNaN( "a" ) ); // true //--- parseFloat parseInt var floatNum = parseFloat("1.2"); var intNum = parseInt( "123" ); console.info( floatNum + " " + typeof( floatNum ) ); console.info( intNum + " " + typeof( intNum ) ); /* 1.2 number 123 number */
JavaScript-面向对象-系统函数-内部类-Math-Date-String-Array-Boolean-Number
标签:
原文地址:http://blog.csdn.net/yongbin668/article/details/51941546