javascript高级程序设计--第五章基本包装类型开始

/*--------基本包装类型
 每当读取一个基本类型(boolean,Number,String)值的时候,后台会创建一个对应的基本包装类型对象,
 var s1 = "some text";
var s2 = s1.substring(2);
//第二行代码访问第一行时,访问过程是读取模式,从内存中读取这个字符串,读取模式访问字符串时会进行下列过程:
//1.创建String类型的实例
//2.在实例上调用指定方法
//3.销毁这个实例
//其实就是这样
var s1 = new String("some text");
var s2 = s1.substring(2);
s1=null;
引用类型和基本包装类型最主要的区别是生存期
使用new操作符创建的引用类型实例,在执行流离开当前作用域之前都在内存中
而自动创建的基本包装类型对象则只存在于一行代码的执行瞬间,然后立即被销毁,这说明我们不能为基本类型添加属性
var s1 =“some text"
s1.color = "red";
alert(s1.color);undefined  这里s1.color=”red"在执行完这句代码后就被销毁了,第三行是重新穿创建的一个String对象,没有color属性
对基本包装类型使用typeof会返回object,所有基本包装类型的对象都会被转化为true
var obj = new Object("some text");
alert(obj instanceof String);true
var num = 2;
alert(num.toString(2));方法里面的参数是返回几进制
var num =10.323;
alert(num.toFixed(2));//后面保留几位小数,而且会四舍五入
var num =10.367;
alert(num.toFixed(2));//10.37
var num =10;
alert(num.toExponential(1));//使用指数表示法表示,里面的参数是保留的小数位数,这个方法写作toExponential
alert(num.toPrecision(2));//这个方法会返回他认为最合适的的格式,里面的参数表示有几个数字不限于小数部分

*-------String类型
var stringValue = "hello world";
alert(stringValue.charAt(1));//返回从零开始的第n个字符
alert(stringValue.charCodeAt(1));//得到第n个字符的的字符编码
alert(stringValue[1]);//也可以由数组的方式访问到

*----字符串操作方法
 1.concat
var stringValue = "hello ";
var result = stringValue.concat("world");//concat连接字符串的作用
alert(result);//hello world
alert(stringValue);//world
concat 可以拼接多个字符串
var stringValue = "hello ";
var result = stringValue.concat("world","HJC");//concat里面有几个参数都可以拼接成字符长串
alert(result);

----基于子字符串创建新字符串substr(),substring(),slice().特别注意substr方法里的第二个参数是返回的个数不是终止位置
var stringValue = "hello world";
alert(stringValue.substring(1,3));//el 返回的位置范围从【1,3)
alert(stringValue.substr(1,3));//ell  返回的位置范围是[1,1+3)第二个参数是返回的字符串个数
alert(stringValue.slice(1,3));//el作用和substrin类似

---只有一个参数的情况
var stringValue = "hello world";
alert(stringValue.substring(1));//返回的是从第一个参数的位置到字符串的结束位置的字符串
alert(stringValue.substr(1));
alert(stringValue.slice(1));

------参数只有一个为负数的情况
var stringValue = "hello world";
alert(stringValue.substring(-2));//hello world说明substring会把所有负值转化为零操作
alert(stringValue.substr(-2));//ld   substr返回从最后往前数的n个字符
alert(stringValue.slice(-2));//ld      和substr的行为类似
------参数第两个为负数
var stringValue = "hello world";
alert(stringValue.substring(2,-5));//h  实践证明,substring只要第二个参数为负数直接置零,那么第一个参数为正,那么返回的字符串为【0,n)
alert(stringValue.substr(1,-3));//空  substr直接将第二个参数为零的直接置零
alert(stringValue.slice(1,-3));//ello wo  如果有个参数为负值,那么这里的范围为【1,stringValue.length+(-3));
-----字符串中查找字串
1.indexof从前往后找,返回开始字符串的位置
var stringValue = "hello world";
alert(stringValue.indexOf("o"));//4
2.lastindexof从后往前找,返回字符串的起始位置
alert(stringValue.lastIndexOf("o"));//7
若没有找到返回-1
示例
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var position = new Array();
var pos = stringValue.indexOf("e");
while (pos>-1){
    position.push(pos);
    pos = stringValue.indexOf("e",pos+1);
}
alert(position);

-----trim()方法删除前置和后缀的所有方法,返回的是原始字符串的副本,原始字符串不变
var stringValue = "     hello world      ";
var newStringValue =stringValue.trim();
alert(stringValue);
alert(newStringValue);//字符串中间的空格不会被删掉

-----字符串大小写转换方法
var stringValue = "Hello World";
1.toLowerCase()
alert(stringValue.toLowerCase());//hello world 转换为小写
2.toLocalLowerCase()
alert(stringValue.toLocaleLowerCase());//hello world 通常结果和toLowerCase()结果一致,在少数特定区别为符号当地语言会有区别
3.toUpperCase()
alert(stringValue.toUpperCase());//HELLO WORLD  转换为大写
4.toLocalUpperCase()
alert(stringValue.toUpperCase());//HELLO WORLD    通常结果和toUpperCase()结果一致,在少数特定区别为符号当地语言会有区别

------字符串的模式匹配
1.match()在字符串上调用这个方法本质上与调用RegExp()和exec()一致,只接收一个参数RegExp对象或者正则表达式
var text = "cat,bat,sat,fat";
var pattern =/.at/;
var matches = text.match(pattern);//本质上与调用pattern.exec(text)一样
alert(matches.index);
alert(matches[0]);
alert(pattern.lastIndex);
2.search()方法和match()一样,只不过里面的正则表达式需要实例化
var text = "cat,bat,sat,fat";
var matchers = text.search(/.at/);//注意这里不需要加引号,这个方法返回第一个匹配项的的位置,如果没有找到匹配项返回-1,从起始
alert(matchers);//位置向后查找
3.replace()接收两个参数(RegExp对象或者字符串,一个字符串或者函数)
如果是字符串只替换第一次出现字符串的地方的字串,如果需要全部替换则需要提供一个正则表达式(并设置全局)
var text = "cat,bat,sat,fat";
var result = text.replace("at","baby");
alert(result);//cbaby,bat,sat,fat
var newresult = text.replace(/at/g,"baby");//一定注意正则表达式不要加引号
alert(newresult);//cbaby,bbaby,sbaby,fbaby

-----replace方法第二个参数可以是函数,在只有一个匹配项的情况下会向这个函数传递三个参数
(模式的匹配项,模式匹配项在字符串中的位置,和原始字符串)
function htmlEscape(text) {
    return text.replace(/[<>"&]/g,function (match,matchindex,originalText) {
       switch (match){
           case "<" :
               return "<";
           case ">":
               return ">";
           case "\"":
               return "&";
           case "&":
               return "&";
       }
    });//正则表达式里面的中括号表示匹配范围
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));//<p class=&greeting&>Hello world!</p>

----split()方法。基于指定的分隔符将一个字符串分隔成多个字符串,并将结果放在数组中。分隔符可以是一个字符串,也可以是一个RegExp
对象,这个方法不会把字符串解析为RegEXp对象.
方法可以接收第二个参数用以指明数组的大小
var colorText = "red,blue,green,yellow";
alert(colorText.split("|"));//red,blue,green,yellow
alert(colorText.split(",",3));//red,blue,green
alert(colorText.split(/[^\,]+/));//,,,,,,,

----localeCompare()
var stringValue = "blue";
alert(stringValue.localeCompare("green"));//-1如果方法里面的参数比字符串的位置在字母表中靠后,则返回-1
alert(stringValue.localeCompare("a"));//1  如果靠前就返回1
alert(stringValue.localeCompare("blue"));//0  //相等返回0
特别注意  实现支持的国家和语言决定了这个行为

---stringCharCode()方法。隶属于String的静态方法,将字符编码转化成字符串
alert(String.fromCharCode(101,102,103,104));//efgh

-------单体内置对象
有ECMAScript实习提供,不依赖于宿主环境的对象,这些对象在ECMAScript程序执行之前已经存在了
1.Global
所有在全局作用域中定义的的属性和函数,都是Global对象的属性如isNaN(),isFinite().
Globla包含的其他方法
        1.URL编码  encodeURI()和encodeURIComponent()
        用特殊的UTF-8编码替换所有无效的字符
        encodeURI()主要用于整个URL的编码,并且不会对特殊字符进行编码。比如冒号,斜杠等
        encodeURIComponent()主要用于对URL中的一段进行编码,对特殊字符会进行编码
        var uri ="https://www.baidu.com/?tn=sitehao123 15";//15前面是下划线换成了空格
        alert(encodeURI(uri));//https://https://www.baidu.com/?tn=sitehao123%2015
        alert(encodeURIComponent(uri));//https%3A%2F%2Fwww.baidu.com%2F%3Ftn%3Dsitehao123%2015

         2.decodeURI只能解析encodeURL编码的字符串
         decodeURIComponent只能解析encodeURIComponent编码的字符串
         var uri ="https%3A%2F%2Fwww.baidu.com%2F%3Ftn%3Dsitehao123%2015";//15前面是下划线换成了空格
         alert(decodeURI(uri));//https%3A%2F%2Fwww.baidu.com%2F%3Ftn%3Dsitehao123 15
         alert(decodeURIComponent(uri));//https://www.baidu.com/?tn=sitehao123 15

        3 eval()相当于一个ECMAScipt解析器,任何要执行的JS代码都可以放在里面执行
        eval("alert("hello")");
        等价于
        alert("hello");
        通过eval()执行的代码被认为是包含该次调用执行环境的一部分,因此被执行的代码具有与该执行环境相同的作用域链。
        例如
        eval("function sayHi(){
                alert();
        }");
        sayHi();也是成立的.
2.window对象
无法直接访问window对象,但是可以将这个全局对象作为window对象的一部分实现,在全局作用域中申明的所有函数和变量都成为了window对象的属性
var color ="red";
function  sayColor() {
    alert(window.color);
}
window.sayColor();//等价于sayColor 说明全局变量是window对象的属性

另外一种情况取得GLOBAl对象
var global = function(){
        return this;//注意这个需要在全局变量下操作
    }();

--------Math对象
1.min和max可以接收任意多的参数,调用函数可以直接求得最大最小值
2.找数组中的最大最小值
Math.max.apply(Math,数组名);//由于apply第二个参数必须是数组,所以这里不能用call
var value =[1.2,3,4,6,5];
alert(Math.max.apply(Math,value));让Math.max下的this指向Math,然后传入数组里面的值即可求解最大值
call和apply的作用就是切换函数的对象上下文(执行环境)
3.舍入方法
Math.ceil()向上舍入
alert(Math.ceil(25.1));//26
Math.floor()向下舍入
Math.round()按照标准的四舍五入
4.random()方法
Math.random()产生一个0到1之间的随机数,不包括0和1
值 = Math.floor(Math.random()*可能值的总数+第一个可能值);
选择一个1到10之间的整数
alert(Math.floor(Math.random()*10+1));
2到10
alert(Math.floor(Math.random()*9+2));
写一个函数通过范围直接求得随机数
function selectFrom(lowerValue,upperValue) {
    var choices = upperValue - lowerValue + 1;
    return Math.floor(Math.random()*choices+lowerValue);
}
var num = selectFrom(2,10);
alert(num);
* 从而可以调用一些方法操作这些数据*/










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值