JS-对象的方法

//String对象方法
// var str = 'hello';
// console.log(str.length); //5
// console.log(str[0]); //h
//str中本身保存的是字符串类型数据,但是在使用时把对象转换为伪对象
//所以能引用string对象的属性
//伪对象:对于基本类型的数据,在读取时,js会把数据转换成相应的伪对象,再读取伪对象(即
//读出来是对象类型,因此有相应的属性)。

//split 用于把一个字符串分割成字符串数组
// var str = 'hello,world'
// var result = str.split('l');
// console.log(result);

// var str = '123012301230456789'
// var result = str.split('123');
// console.log(result);
// ["", "0", "0", "0456789"]

// var str = '123012301230456789'
// var result = str.split('a');
// console.log(result);
//["123012301230456789"]

// var str = '123012301230456789'
// var result = str.split('');
// console.log(result);
//["1", "2", "3", "0", "1", "2", "3", "0", "1", "2",
//"3", "0", "4", "5", "6", "7", "8", "9"]


//charCodeAt() 返回指定位置的字符的Unicode编码,结果肯定是数字
// var str = 'hello';
// console.log(str.charCodeAt(1)); //101

// //concat() 连接字符串,把它的所有参数转换成字符串,
//然后按顺序连接到字符串 stringObject 的尾部,并返回连接后的字符串
// 同Array用法 注:+也有相同的功能
// var str = 'hello';
// var result = str.concat('js','haha')
// console.log(result);
// //hellojshaha
// console.log(str);
//hello

//indexOf() 返回某个字符串片段在字符串中首次出现的位置,从左到右
// var str = 'hello';
// var result = str.indexOf('el');
// console.log(result); //1

// var str = 'hello'; //从左到右进行搜索,搜索的第一个l的角标
// var result = str.indexOf('l');
// console.log(result); //2

// var str = 'hello'; //没有检索到字符串,返回值为-1
// var result = str.indexOf('lll');
// console.log(result); //-1

// var str = 'hello, world';
// var result = str.indexOf('l',6);
// //6代表从第几位开始检索
// console.log(result); //10

//lastIndexOf() 从右到左搜索

//replace() 替换字符串中的字符,只会替换第一个满足的字符,
//如果全要替换,需要使用正则表达式
// var str = 'hello, world';
// var result = str.replace('l',0);
// // l:想要替换的字符 0:要替换成什么
// console.log(result);
// //he0lo,world
// console.log(str);
//hello,world


// slice() 截取片段,同Array
// var str = 'hello, world';
// var result = str.slice(3,-1);
// console.log(result); //lo, worl

// toUpperCase() 转换大写
// var str = 'helLo, worLd';
// console.log(str);
// //helLo, worLd
// var result = str.toUpperCase();
// console.log(result);
// //HELLO, WORLD
// console.log(str);
// helLo, worLd

//toLowerCase() 转换小写

// Number对象的方法 (万物皆对象,使用时都是对象)
//toString() 任何一个对象都拥有 (js底层做的)
// var a = '123';
// var b = 100;
// var result = a + b;
// //1、把a和b转换成相应的伪对象进行读取
// //2、由于+号代表的是字符串连接的操作符,此时会调用两个伪对象的toString()
// //方法,把两个方法的返回值拼接在一起
// console.log(result); //123100

//valueOf() 将字符串转换为数字 ,任何一个对象都拥有 (js底层做的)
// var a = '123';
// var b = 100;
// var result = a - b;
// //—只有加减运算
// console.log(result); //23

//toFixed() 指定保留几位小数,会四舍五入,结果为字符串
// var num = 1.23456;
// console.log(num.toFixed(2)); //1.23
// console.log((1.2345).toFixed(2)); //1.23 方法二


//toExExponential() 把结果转换为指数计数法,结果为字符串
// var num = 123456.789;
// console.log(num.toExponential()); //1.23456789e+5

//toPrecision 指定指数计数法的指定位数
// var num = 123456.789;
// console.log(num.toPrecision(2)); //1.2e+5

// Math对象
// 属性:
// PI:返回圆周率
// console.log(Math.PI); //3.141592653589793

// 方法:
// abs(x) 返回数的绝对值
// console.log(Math.abs(10)); //10
// console.log(Math.abs(-10)); //10

//min(x,y) max(x,y) 比起if判断,低版本IE中性能好,
// console.log(Math.min(10,12)); //10
// console.log(Math.max(10,12)); //12

//ceil(x) 对数进行上舍入
// console.log(Math.ceil(10.01)); //11
// console.log(Math.ceil(10.00)); //10

//floor(x) 对数进行向下取整
// console.log(Math.floor(10.99)); //10
// console.log(Math.floor(10.00)); //10
//
// //round(x) 四舍五入
// console.log(Math.round(10.4)); //10
// console.log(Math.round(10.6)); //11

//random(x) 生成0-1之间随意的小数
// var num = Math.random();
// console.log(num); //0.9598256822289126

//生成0-20之间随意的数
// 先随意生成0-1的小数,乘以21,再向下进行取整
// console.log(Math.floor(Math.random()*20));
// 生成1-20之间随意的数
// 先随意生成0-1的小数,乘以20,再向上进行取整

// Date对象 表示日期或时间
// var myDate = new Date(); 创建Date的方式,没有字面量方式
// console.log(myDate); //Wed Oct 19 2016 10:54:59 GMT+0800 (中国标准时间)

// 方法:
// Date() 返回当天的日期和时间
// console.log(Date()); //Wed Oct 19 2016 10:56:44 GMT+0800 (中国标准时间)


//getDate() 返回是几号
// var myDate = new Date();
// console.log(myDate.getDate());
//
// //getMonth() 返回时几月 0为一月
// var myDate = new Date();
// console.log(myDate.getMonth());
//
// //getFullYear() 返回年份
// var myDate = new Date();
// console.log(myDate.getFullYear());
//
// //getDay() 返回星期几 0为周日
// var myDate = new Date();
// console.log(myDate.getDay());

//getHours() 返回小时数 getMinutes/getseconds/getMilliseconds获取毫秒数
//产生时间戳,获取唯一
//(低版本浏览器只能精确到10毫秒或30毫秒)
// var myDate = new Date();
// console.log(myDate.getHours());

//getTimezoneOffset() 获取本地时间与标准时间的时间差(分钟)
// var myDate = new Date();
// console.log(myDate.getTimezoneOffset());

//getUTCHours() 获取标准时间的小时
// var myDate = new Date();
// console.log(myDate.getUTCHours());

//setHours() 设置几点
// var myDate = new Date();
// myDate.setHours(10); //设置当前时间为10点
// console.log(myDate.getUTCHours());

//toString 把当前日期转换为字符串 ,格式为英文
// var myDate = new Date();
// console.log(myDate.toString());

//toLocaleString() 按照本地习惯显示当前日期
// var myDate = new Date();
// console.log(myDate.toLocaleString());

//toTimeString() 只获取时间
// var myDate = new Date();
// console.log(myDate.toTimeString());

//toLocaleTimeString() 按照本地习惯
// var myDate = new Date();
// console.log(myDate.toLocaleTimeString());

posted @ 2016-10-23 08:35  杜润慈前端博客  阅读(542)  评论(0编辑  收藏  举报