美文网首页
JS - 对象

JS - 对象

作者: 恒星的背影 | 来源:发表于2018-11-14 09:07 被阅读0次

字符串是基本类型
基本类型的值是一经确定就不可变的
基本类型的比较是值的比较
五个false值:'', 0, NaN, null, undefined

全局对象 global / window

  • ecma 规定:
    parseInt()
    Number()
    String()
    Boolean()
    Object()
  • 浏览器私有:
    alert()
    confirm()
    console()
    document
    history

Number(), String(), Boolean()

  • Number()
Number('2')    类型转换
var n = new Number(2)    // 堆内存    
var n = 2    // 栈内存
(1).toString()    // 1
(new Number(1)).toString()    // 1

数值可以直接调用 toString 等方法的原因是:调用时JS会创建一个临时对象,然后使用该对象的方法,结束后临时对象被垃圾回收
验证上面的说法:

var n = 1
n.xxx = 2
console.log(n.xxx)    // undefined

给 n.xxx 赋值没有报错,但是输出 n.xxx 是 undefined,可以用上面的说法解释得通

  • String()
var s1 = 'apple'
var s2 = new String('apple')

s1[1]
s1.charAt(1)    

这两种使用方式都用到了临时对象

  • Boolean()
var f1 = false;
var f2 = new Boolean(false);
if(f1) { console.log('1') } ;
if(f2) { console.log('2') } ;

由于 f2 是对象,所以 if 语句判断结果为 true

super

super 等价于 Object.getPrototypeOf(this)this.__proto__
但是 this 的指向并不固定,而 super 的指向是固定的
只能在对象方法中使用,否则直接报错

其它

  • 获取,改变 __proto__
    Object.setPrototypeOf()
    Object.getPrototypeOf()

参考

【译】《Understanding ECMAScript6》- 第三章-Object - 才子锅锅 - 博客园

相关文章

网友评论

      本文标题:JS - 对象

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