【JS学习笔记】基本包装类型

来源:互联网 发布:淘宝如何修改会员名 编辑:程序博客网 时间:2024/04/30 05:25
晓石头的博客
邮箱:178673693@qq.com

转载请注明出处,原文链接:http://blog.csdn.net/qiulanzhu/article/details/50663553


//按值传递,传递的参数是引用类型function box(obj){obj.name = "yi";}var big = new Object();box(big);//按值传递,传递的参数是引用类型document.write(big.name +"<br>");//yi//类型判断//基本类型检测用typeof//对象类型检测用instanceofvar type1 = [1,2,3,4,5];document.write(type1 instanceof Array);var type2 = {};document.write(type2 instanceof Object);var type3 = /g/;document.write(type3 instanceof RegExp);document.write("<br>");/*基本包装类型*///String、Boolean、Number三种基本包装类型//使用new创建以上三种类型的对象时,可以给自己添加属性和方法(字面量创建时不能添加),建议不这么使用,因为这样不容易区分是基本类型还是引用类型。//Number类型的静态属性document.write(Number.MAX_VALUE +"<br>");document.write(Number.MIN_VALUE +"<br>");//String对象属性//length,constructor,prototype//String字符方法var str = 'qiuyi';document.write(str.charAt(1));//返回指定索引位置的字符document.write(str.charCodeAt(1));//返回指定索引位置的字符的uicode编码document.write(str[1]);//慎用,ie浏览器不支持var str1 = 'qiuyiis very activity!';var str2 = 'good';//concatdocument.write(str1.concat(str2) +'<br>');//slice//正数document.write(str1.slice(2,4));//[索引2,索引4);左闭右开document.write(str1.substring(2,4));//同上document.write(str1.substr(2,4) +'<br>');//索引2开始的连续4个字母//负数document.write(str1.slice(-3));//显示最后三位(str1.length-3开始)document.write(str1.substring(-1));//负数全部显示document.write(str1.substr(-3));//显示最后三位(str1.length-3开始)document.write(str1.slice(3,-1));//从索引三到倒数第一位document.write(str1.substring(3,-1) +'<br>');//第二位<0,则直接变成0,变换顺序成[0,3)document.write(str1.substr(3,-1));//[0,3)无效var phone = 'my book is very nice!';document.write(phone.indexOf('i'));//搜索第一个出现i的地方document.write(phone.indexOf('i',9));//从指定位置开始,搜索第一个出现i的地方document.write(phone.lastIndexOf('i'));//从后往前搜索,返回第一次出现i的地方document.write(phone.lastIndexOf('i',10));//从指定的位置往前搜索//ps:如果没有找到指定的字符串,则返回-1//大小写转换var desk = 'cat CAT dog DOG';document.write(desk.toLowerCase());document.write(desk.toUpperCase());//字符串的模式匹配(多用于正则表达式中)var clothes = 'Mr.Qiu is Qiu';document.write(clothes.match('is'));//匹配成功返回匹配的字符串document.write(clothes.search('is'));//和indexOfdocument.write(clothes.replace('is', 'IS'));//IS 替换 isdocument.write(clothes.split(' '));//以空格分割字符串//其它方法//fromCharCode(ascii);输出ascii码对应的值//localeCompare(str1, str2); 比较两个字符串


0 0
原创粉丝点击