js基础(内置对象)

如果直接使用构造函数创建一个Date对象,则会封装成当前代码的执行时间

创建一个指定时间的Date对象,需要在构造函数中传递一个表示时间的字符串作为参数  日期格式:月份/日/年   时:分:秒

<script type="text/javascript">

var d = new Date();
var d2 = new Date("5/09/2018 11:10:30");
/*
 * getDate()
 *        - 获取当前日期对象是几日
 */
console.log("当前时间的日期是:"+d.getDate());
console.log("指定时间的日期是:"+d2.getDate());

/*
 * getDay()
 *        - 获取当前日期对象时周几
 *        - 会返回一个0-6的值
 *                  0 表示周日
 *                  1表示周一
 *                  。。。
 */
console.log("当前时间是周:"+d.getDay());
console.log("指定时间是周:"+d2.getDay());

/*
 * getMonth()
 * d2 = new Date("12/18/2011 11:10:30");
 * - 获取当前时间对象的月份
 *        - 会返回一个0-11的值
 *                  0 表示1月
 *                  1 表示2月
 *                  11 表示12月
 */
console.log("当前时间的月份是:"+d.getMonth());
console.log("指定时间的月份是:"+d2.getMonth());

/*
* getFullYear()
*        - 获取当前日期对象的年份
*/
console.log("当前时间的年数是:"+d.getFullYear());
console.log("指定时间的年数是:"+d2.getFullYear());

/*
 * getTime()
 *        - 获取当前日期对象的时间戳
 *        - 时间戳,指的是从格林威治标准时间的1970年1月1日,0时0分0秒
 *                  到当前日期所花费的毫秒数(1秒 = 1000毫秒)
 *        - 计算机底层在保存时间时使用都是时间戳
 */
console.log("当前时间是:"+d.getTime());
console.log("指定时间是:"+d2.getTime());

//获取当前的时间戳
var start = Date.now();
console.log("当前时间是:"+ start);
</script>

Math对象

/*
 * 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));
          //console.log(Math.round(Math.random()*20));
          
          //console.log(Math.round(Math.random()*9)+1);
          //console.log(Math.round(Math.random()*8)+2);
          
          //生成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);
console.log(max);
/*
* Math.pow(x,y)
*        返回x的y次幂
*/
console.log(Math.pow(12,3));
/*
* Math.sqrt()
*  用于对一个数进行开方运算
*/
console.log(Math.sqrt(2));

包装类:js提供了三个包装类:String、Number、Boolean,通过者三个包装类可以将基本类转换成对象

console.log(typeof new Number(2324));//object
console.log(typeof new Boolean(true));//object

基本类型:String、Number、Boolean、Null、Undefined

引用类型:Object

在实际中,一般不使用基本数据类型的对象,如果使用,可能带来一些不可预期的后果

var number = new Number(1);
number.str = "success";
console.log(number);//Number {1, str: "success"}
var bool1 = new Boolean(true);
var bool2 = new Boolean(true);
console.log(bool2 == bool1);//false  两个不同的对象
var b = new Boolean(false);
if(b){
         console.log("条件是真值"); //这里会运行
}
/*
* 方法和属性之能添加给对象,不能添加给基本数据类型
*        当我们对一些基本数据类型的值去调用属性和方法时,
*                  浏览器会临时使用包装类将其转换为对象,然后在调用对象的属性和方法
*                  调用完以后,在将其转换为基本数据类型
*/
var s = 123;
s = s.toString();
s.hello = "你好";

console.log(s.hello);  //undefined
console.log(typeof s); //string

字符串在底层是字符数组的形式进行保存

/*
 * length属性
 *        - 可以用来获取字符串的长度
 */
 var str = "success";
console.log(str.length);
console.log(str[5]);
/*
 * charAt()
 *        - 可以返回字符串中指定位置的字符
 *        - 根据索引获取指定的字符       
 */
var result = str.charAt(4);
console.log(result); //e
/*
 * charCodeAt()
 *        - 获取指定位置字符的字符编码(Unicode编码)
 */
console.log(str.charCodeAt(0));
/*
* String.formCharCode()
*        - 可以根据字符编码去获取字符
*/
console.log(String.fromCharCode(0x2692));
/*
 * concat()
 *        - 可以用来连接两个或多个字符串
 *        - 作用和+一样
 */
 console.log(str.concat("hello","world"));//successhelloworld
 /*
 * indexof()
 *        - 该方法可以检索一个字符串中是否含有指定内容
 *        - 如果字符串中含有该内容,则会返回其第一次出现的索引
 *                  如果没有找到指定的内容,则返回-1
 *        - 可以指定一个第二个参数,指定开始查找的位置
 * 
 * lastIndexOf();
 *        - 该方法的用法和indexOf()一样,
 *                  不同的是indexOf是从前往后找,
 *                  而lastIndexOf是从后往前找
 *        - 也可以指定开始查找的位置
 */
console.log(str.indexOf("e",1));     //4
console.log(str.lastIndexOf("s",7)); //6
/*
 * slice()
 *        - 可以从字符串中截取指定的内容
 *        - 不会影响原字符串,而是将截取到内容返回
 *        - 参数:
 *                  第一个,开始位置的索引(包括开始位置)
 *                  第二个,结束位置的索引(不包括结束位置)
 *                            - 如果省略第二个参数,则会截取到后边所有的
 *                  - 也可以传递一个负数作为参数,负数的话将会从后边计算
 */
str = "abcdefghijk";
result = str.slice(1,4);
console.log(result);       //bcd
result = str.slice(1,-1);  //bcdefghij
console.log(result);
/*
 * substring()
 *        - 可以用来截取一个字符串,可以slice()类似
 *        - 参数:
 *                  - 第一个:开始截取位置的索引(包括开始位置)
 *                  - 第二个:结束位置的索引(不包括结束位置)
 *                  - 不同的是这个方法不能接受负值作为参数,
 *                            如果传递了一个负值,则默认使用0
 *                  - 而且他还自动调整参数的位置,如果第二个参数小于第一个,则自动交换
 */

result = str.substring(0,1);
console.log(result);    //a
/*
 * substr()
 *        - 用来截取字符串
 *        - 参数:
 *                  1.截取开始位置的索引
 *                  2.截取的长度
 */
str = "abcdefg";
result = str.substr(3,2);
console.log(result);   //de
/*
* split()
*        - 可以将一个字符串拆分为一个数组
*        - 参数:
*                  -需要一个字符串作为参数,将会根据该字符串去拆分数组
*/
str = "abcbcdefghij";
result = str.split("d");
console.log(result);  //["abcbc", "efghij"]
/*
* 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素
*/
result = str.split("");
console.log(result);  //["a", "b", "c", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
                           
/*
* toUpperCase()
*        - 将一个字符串转换为大写并返回
*/
str = "abcdefg"; 
result = str.toUpperCase();
console.log(result);

/*
* toLowerCase()
*        -将一个字符串转换为小写并返回
*/
str = "ABCDEFG";
result = str.toLowerCase();
console.log(result);

          

猜你喜欢

转载自www.cnblogs.com/lzb0803/p/9019390.html
今日推荐