JavaScript高级编程———基本包装类型和单体内置对象
<script> var stringObject = new String("hello world"); document.write("字符:" + stringObject + "<br>"); document.write("每个字符也仍然算一个字符:" + stringObject.length + "<br>"); document.write("以单字符字符串的形式返回给定位置的那个字符:" + stringObject.charAt(1) + "<br>"); document.write("以单字符字符串的形式返回字符编码:" + stringObject.charCodeAt(1) + "<br>"); //获取A_Z的大写字符 var A_Z = []; for (var i = 65; i < 91; i++) { var strA = String.fromCharCode(i); A_Z.push(strA); } console.log(A_Z); document.write("输出26个大写字符:" + A_Z + "<br>"); var stringValue = "hello "; var result = stringValue.concat("world"); document.write("stringValue拼接的原始数据:" + stringValue + ",使用concat用于将一或多个字符拼接起来,返回拼接得到的新字符串:" + result + "<br>"); document.write("原始的stringValue的值保持不变:" + stringValue + "<br>"); var stringValue = " hello world "; var trimmedStringValue = stringValue.trim(); //删除前置及后缀的所有空格 document.write("删除前置及后缀的所有空格:" + trimmedStringValue + "<br>"); var trimLeftmedStringValue = stringValue.trimLeft(); //删除开头的空格 document.write("删除前置的空格:" + trimLeftmedStringValue + "<br>"); var trimRightmedStringValue = stringValue.trimRight(); //删除后缀的空格 document.write("删除后缀的空格:" + trimRightmedStringValue + "<br>"); //字符大小写转换方法 var stringtoLocalUpperCase = stringValue.toLocaleUpperCase(); var stringtoUpperCase = stringValue.toUpperCase(); document.write("大写转换:" + stringtoLocalUpperCase + "<br>"); var stringtoLocaleLowerCase = stringValue.toLocaleLowerCase(); var stringtoLowerCase = stringValue.toLowerCase(); document.write("小写转换:" + stringtoLocaleLowerCase + "<br>"); var uri = "http://www.wrox.com/illegalvalue.htm#start"; //alert(encodeURI(uri)); //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start" //alert(encodeURIComponent(uri)); document.write("加密encodeURI:" + encodeURI(uri) + "<br>"); document.write("加密encodeURIComponent:" + encodeURIComponent(uri) + "<br>"); //Math对象 var max = Math.max(3, 54, 32, 16); document.write("max的最大值:" + max + "<br>"); var min = Math.min(3, 54, 32, 16); document.write("min的最小值:" + min + "<br>"); //找数组的最大或者最小值,可以调用apply使用 var valueArray = [1, 2, 3, 4, 5, 6, 7, 8]; var maxApply = Math.max.apply(Math, valueArray); var minApply = Math.min.apply(Math, valueArray); document.write("获取数组的最大值:" + maxApply + "<br>"); document.write("获取数组的最小值:" + minApply + "<br>"); /*舍入方法 Math.ceil() 执行向上舍入,始终将数值向上舍入为最接近的整数 Math.floor() 执行向下舍入,始终将数值向下舍入为最接近的整数 Math.round() 执行标准舍入,即总是将数值四舍五入为最接近的整数 */ document.write("Math.ceil() 执行向上舍入:" + Math.ceil(25.9) + "<br>"); document.write("Math.floor()执行向下舍入:" + Math.floor(25.9) + "<br>"); document.write("Math.round()执行标准舍入:" + Math.round(25.1) + "<br>"); //随机1-10的数值 var num = Math.floor(Math.random() * 10 + 1); //随机2-10的数值 var numtwo = Math.floor(Math.random() * 9 + 2); function selectFrom(lowerValue, upperValue) { var choices = upperValue - lowerValue + 1; return Math.floor(Math.random()*choices+lowerValue); } var numForm = selectFrom(2, 10); //alert(numForm); //介于2和10之间的一个数值,总值和第一个出现的值 document.write("介于2和10之间的一个数值:" + numForm + "<br>"); var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"]; var color = colors[selectFrom(0, colors.length - 1)]; document.write("取随机数组的颜色:" + color + "<br>"); </script>