<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> 1. Date对象 /* * Date对象 * - 在JS中使用Date对象来表示一个时间 */ //创建一个Date对象 //如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间 var d = new Date(); //创建一个指定的时间对象 //需要在构造函数中传递一个表示时间的字符串作为参数 //日期的格式 月份/日/年 时:分:秒 var d2 = new Date("2/18/2011 11:10:30"); /* * getDate() * - 获取当前日期对象是几日 */ var date = d2.getDate(); /* * getDay() * - 获取当前日期对象时周几 * - 会返回一个0-6的值 * 0 表示周日 * 1表示周一 * 。。。 */ var day = d2.getDay(); /* * getMonth() * d2 = new Date("12/18/2011 11:10:30"); * - 获取当前时间对象的月份 * - 会返回一个0-11的值 * 0 表示1月 * 1 表示2月 * 11 表示12月 */ var month = d2.getMonth(); /* * getFullYear() * - 获取当前日期对象的年份 */ var year = d2.getFullYear(); //console.log(d2); //console.log("date = "+date); //console.log("day = "+day); //console.log("month = "+month); //console.log(year); /* * getTime() * - 获取当前日期对象的时间戳 * - 时间戳,指的是从格林威治标准时间的1970年1月1日,0时0分0秒 * 到当前日期所花费的毫秒数(1秒 = 1000毫秒) * - 计算机底层在保存时间时使用都是时间戳 */ var time = d2.getTime(); //console.log(time/1000/60/60/24/365); /*var d3 = new Date("1/1/1970 0:0:0"); time = d3.getTime(); console.log(time);*/ //利用时间戳来测试代码的执行的性能 //获取当前的时间戳 var start = Date.now(); for(var i=0 ; i<100 ; i++){ console.log(i); } var end = Date.now(); console.log("执行了:"+(end - start)+"毫秒"); 2. Math对象 /* * Math * - Math和其他的对象不同,它不是一个构造函数, * 它属于一个工具类不用创建对象,它里边封装了数学运算相关的属性和方法 * - 比如 * Math.PI 表示的圆周率 */ //console.log(Math.PI); /* * abs()可以用来计算一个数的绝对值 */ //console.log(Math.abs(-1)); /* * Math.ceil() * - 可以对一个数进行向上取整,小数位只有有值就自动进1 * Math.floor() * - 可以对一个数进行向下取整,小数部分会被舍掉 * Math.round() * - 可以对一个数进行四舍五入取整 */ //console.log(Math.ceil(1.1)); //console.log(Math.floor(1.99)); //console.log(Math.round(1.4)); /* * Math.random() * - 可以用来生成一个0-1之间的随机数 * - 生成一个0-10的随机数 * - 生成一个0-x之间的随机数 * Math.round(Math.random()*x) * * - 生成一个1-10 * - 生成一个x-y之间的随机数 * Math.round(Math.random()*(y-x)+x) */ /*for(var i=0 ; i<100 ; i++){ //console.log(Math.round(Math.random()*10)); //0-10 //console.log(Math.round(Math.random()*20)); //0-20 //console.log(Math.round(Math.random()*9)+1); //Math.round(Math.random()*9) 0-9 加1就是1-10 //console.log(Math.round(Math.random()*8)+2); //2-10 //生成1-6之间的随机数 console.log(Math.round(Math.random()*5+1)); }*/ /* * max() 可以获取多个数中的最大值 * min() 可以获取多个数中的最小值 */ var max = Math.max(10,45,30,100); var min = Math.min(10,45,30,100); //console.log(min); /* * Math.pow(x,y) * 返回x的y次幂 */ //console.log(Math.pow(12,3)); /* * Math.sqrt() * 用于对一个数进行开方运算 */ console.log(Math.sqrt(2)); 3. 包装类 /* * 基本数据类型 * String Number Boolean Null Undefined * 引用数据类型 * Object * * 在JS中为我们提供了三个包装类,通过这三个包装类可以将基本数据类型的数据转换为对象,首字母大写,都是构造函数 * String() * - 可以将基本数据类型字符串转换为String对象 * Number() * - 可以将基本数据类型的数字转换为Number对象 * Boolean() * - 可以将基本数据类型的布尔值转换为Boolean对象 * 但是注意:我们在实际应用中不会使用基本数据类型的对象, * 如果使用基本数据类型的对象,在做一些比较时可能会带来一些不可预期的结果 */ //创建一个Number类型的对象 //num = 3; var num = new Number(3); //把3转换成对象,值还是3 var num2 = new Number(3); //num2 不等于num var str = new String("hello"); var str2 = new String("hello"); var bool = new Boolean(true); var bool2 = true; //bool等于bool2,但不全等 //向num中添加一个属性 num.hello = "abcdefg"; //console.log(str === str2); var b = new Boolean(false) //对象转成bool,永远是true /*if(b){ //一直运行 alert("我运行了~~~"); }*/ /* * 方法和属性之能添加给对象,不能添加给基本数据类型 * 当我们对一些基本数据类型的值去调用属性和方法时, * 浏览器会临时使用包装类将其转换为对象,然后在调用对象的属性和方法 * 调用完以后,在将其转换为基本数据类型 */ var s = 123; newS = s.toString(); //临时性转化成对象1,调用toString方法,转换成字符串后,赋值给newS,对象1销毁 s.hello = "你好"; //临时转换对象2,添加属性后,对象2销毁,编译不报错,读取报错 console.log(s.hello); //读取对象2属性,对象2已经销毁,undefined //console.log(typeof s); 4. String()字符串方法 //创建一个字符串 var str = "Hello Atguigu"; /* * 在底层字符串是以字符数组的形式保存的 * ["H","e","l"] */ /* * length属性 * - 可以用来获取字符串的长度 */ //console.log(str.length); //console.log(str[5]); /* * charAt() * - 可以返回字符串中指定位置的字符 * - 根据索引获取指定的字符 */ str = "中Hello Atguigu"; var result = str.charAt(6); //浏览器会创建一个临时的string对象,调用完后销毁,不会改变原字符串 var result = str.charAt(0); //20013 中的Unicode编码 /* * charCodeAt() * - 获取指定位置字符的字符编码(Unicode编码) */ result = str.charCodeAt(0); /* * String.formCharCode() 用String对象直接使用 * - 可以根据字符编码去获取字符 */ result = String.fromCharCode(0x2692); //打印特殊字符 /* * concat() * - 可以用来连接两个或多个字符串 * - 作用和+一样 */ result = str.concat("你好","再见"); /* * indexof() * - 该方法可以检索一个字符串中是否含有指定内容 * - 如果字符串中含有该内容,则会返回其第一次出现的索引 * 如果没有找到指定的内容,则返回-1 * - 可以指定一个第二个参数,指定开始查找的位置 * * lastIndexOf(); * - 该方法的用法和indexOf()一样, * 不同的是indexOf是从前往后找, * 而lastIndexOf是从后往前找 * - 也可以指定开始查找的位置 */ str = "hello hatguigu"; result = str.indexOf("h",1); result = str.lastIndexOf("h",5); /* * slice() * - 可以从字符串中截取指定的内容 * - 不会影响原字符串,而是将截取到内容返回 * - 参数: * 第一个,开始位置的索引(包括开始位置) * 第二个,结束位置的索引(不包括结束位置) * - 如果省略第二个参数,则会截取到后边所有的 * - 也可以传递一个负数作为参数,负数的话将会从后边计算 */ str = "abcdefghijk"; result = str.slice(1,4); result = str.slice(1,-1); /* * substring() * - 可以用来截取一个字符串,可以slice()类似 * - 参数: * - 第一个:开始截取位置的索引(包括开始位置) * - 第二个:结束位置的索引(不包括结束位置) * - 不同的是这个方法不能接受负值作为参数, * 如果传递了一个负值,则默认使用0 * - 而且他还自动调整参数的位置,如果第二个参数小于第一个,则自动交换 str.substring(0,1); 等价于 str.substring(1,0); */ result = str.substring(0,1); /* * substr() * - 用来截取字符串 * - 参数: * 1.截取开始位置的索引 * 2.截取的长度 */ str = "abcdefg"; result = str.substr(3,2); /* * split() * - 可以将一个字符串拆分为一个数组 * - 参数: * -需要一个字符串作为参数,将会根据该字符串去拆分数组 */ str = "abcbcdefghij"; result = str.split("d"); /* * 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素 */ result = str.split(""); //console.log(Array.isArray(result)); //console.log(result[0]); console.log(result); str = "abcdefg"; /* * toUpperCase() * - 将一个字符串转换为大写并返回 */ result = str.toUpperCase(); str = "ABCDEFG"; /* * toLowerCase() * -将一个字符串转换为小写并返回 */ result = str.toLowerCase(); //console.log(result); </script> </head> <body> </body> </html>
Date/Math/String对象的函数
猜你喜欢
转载自www.cnblogs.com/luoxuw/p/11451128.html
今日推荐
周排行