美文网首页JavaScript高级程序设计Web前端之路
第五章(5):基本包装类型和单体内置对象

第五章(5):基本包装类型和单体内置对象

作者: 日暮途远_ | 来源:发表于2017-06-18 17:59 被阅读6次

基本包装类型

每当读取一个基本类型值的时候,后台就会创建一个与之对应的基本包装类型。基本包装类型顾名思义就是对基本类型的一层包装,成为一个特殊的引用类型。然后具备一些方法和属性。方便其使用。

/**
 * 1和2的操作类似,实际在1执行的时候默认执行的是2的操作
 *
 * @type {string}
 */
// 1 基本类型
var str = 'hello'
var str1 = str.concat(', world')

// 2
var str = new String('hello')
var str1 = str.concat(', world')
str1 = null

/**
 * 基本类型和基本包装类型的区别是:基本包装类型在离开当前作用域之前是一直存在的,而基本类型背后默认自动创建的基本包装类型的实例在执行完之后就销毁了
 * @type {string}
 */
var s1 = 'hello'
var s2 = new String('hello')
s1.age = 18
console.log('s1: ', s1) // s1:  hello
s2.age = 18
console.log('s2: ', s2) // s2:  String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", age: 18, length: 5, [[PrimitiveValue]]: "hello"}

/**
 * boolean基本包装类型
 * @type {boolean}
 */
var b1 = true
var b2 = new Boolean(true)
console.log(typeof b1)  // boolean
console.log(typeof b2)  // object

/**
 * Number基本包装类型
 * toFixed 可以接受一个参数,代表要保留的小数位,能够自动四舍五入。
 * @type {number}
 */
var n1 = 12346.678
console.log(n1.toFixed(2))  // 12346.68

/**
 * String 基本包装类型
 * @type {string}
 */
var msg = 'hello,日暮途远'
console.log(msg.length) // 10 即使是汉字,也只占1位
console.log(msg.charAt(6))  // 日 (返回给定位置的字符)
console.log(msg.charCodeAt(6))  // 26085  (返回给定位置的字符编码)
console.log(msg.concat('!'))  // hello,日暮途远! (和数组的concat方法类似)

console.log(msg.slice(5,7)) // ,日 (和数组的slice方法类似)
console.log(msg.substring(5,7)) // ,日 (和slice方法类似)
console.log(msg.substr(5,7)) // ,日暮途远 (第一位是起始位置,第二位是长度)

console.log(msg.indexOf('l')) // 2  (和数组的indexOf方法类似)
console.log(msg.lastIndexOf('l')) // 3  (和数组的lastIndexOf方法类似)

var brief = ' h e l l o '
console.log(brief.trim()) // 删除开头和结尾的空格,对原字符没有影响

//在之前的正则表达式章节已经介绍过,这里就不多说了
var pattern1 = / /g
console.log(brief.match(pattern1))  // [" ", " ", " ", " ", " ", " "]
console.log(brief.replace(pattern1,""))  // hello

//可以接受一个或多少字符编码然后转化成字符
console.log(String.fromCharCode(97,98)) // ab

内置对象

由ECMAScript实现提供的,不依赖于宿主环境的对象,这些对象在ECMAScript程序之前就已经存在,例如Object,Array,String。还有两个单体内置对象:Global和Math

  • Global
    Global对象是不存在的。但是你可以理解为它是上帝。所有在全局环境中定义的属性和函数,都是Global对象的属性和方法。Global还有以下几个方法
// Global
/**
 * encodeURI 不会对本身的特殊字符进行编码,只会对空格和非字母(汉字)进行编码
 * encodeURIComponent 会对所有非字母的字符进行编码
 * @type {string}
 */
var url = 'http://www.baidu.com/index?a=1&name=日暮途远 ——————'
var enU = encodeURI(url)  // http://www.baidu.com/index?a=1&name=%E6%97%A5%E6%9A%AE%E9%80%94%E8%BF%9C%20%E2%80%94%E2%80%94%E2%80%94%E2%80%94%E2%80%94%E2%80%94
var enUC = encodeURIComponent(url)  // http%3A%2F%2Fwww.baidu.com%2Findex%3Fa%3D1%26name%3D%E6%97%A5%E6%9A%AE%E9%80%94%E8%BF%9C%20%E2%80%94%E2%80%94%E2%80%94%E2%80%94%E2%80%94%E2%80%94
console.log(enU)
console.log(enUC)

//解码
console.log(decodeURI(enU)) // http://www.baidu.com/index?a=1&name=日暮途远 ——————
console.log(decodeURIComponent(enUC)) // http://www.baidu.com/index?a=1&name=日暮途远 ——————

/**
 * eval只接受一个参数,要执行的ECMAScript的字符串
 * @type {string}
 */
var str = 'console.log("hello, world")'
console.log(eval(str))  // hello, world
console.log(eval('hello, world')) // Uncaught ReferenceError: hello is not defined
  • Math
    Math对象保存了一些方便于数学计算的属性和方法。
//Math
var arr = [1,2,3,4,5,6]
console.log(Math.max.apply(Math, arr))  // 6 获取最大值
console.log(Math.min.apply(Math, arr))  // 1 获取最小值

var num = 123.578
console.log(Math.ceil.call(Math, num))  // 124 向上取整
console.log(Math.floor.call(Math, num)) // 123 向下取整
console.log(Math.round.call(Math, num)) // 124 四舍五入
/**
 * 一个范围内取随机数
 * ran = Math.floor(Math.random() * 个数 + 最小数)
 * @type {number}
 */
var ran = Math.floor(Math.random() * 10 + 1)
console.log(ran)  //  1 <= ran <= 10

另外还有例如sin, cos等方法,这里就不一一列举了。

引用

javascript高级程序设计

相关文章

网友评论

    本文标题:第五章(5):基本包装类型和单体内置对象

    本文链接:https://www.haomeiwen.com/subject/yufwqxtx.html