全局对象
ECMAScript规定全局对象叫做global,但是浏览器把window作为全局对象(浏览器先存在的)
window就是一个哈希表,有很多属性;window的属性就是全局变量。
| (ECMAScript规定) | (chrom,firefox等特有的) |
|---|---|
| global.parseInt() | window.alert |
| global.parseFloat() | window.prompt |
| global.Number() | window.console |
| global.String() | window.confirm |
| global.Boolean() | window.document |
| global.Object() |
全局函数
Number
比较下
① var a = 1;
② var b = new Number(1) 的不同;
一句话概括就是内存不同。
① 是在stack栈内存中直接储存。
② 是在stack栈内存中储存地址,然后在heap堆内存中存该地址的对象。
所以说 var a = 1;和 var b = new Number (1)都可以表示。
那么var a = 1 这个是基础类型那怎么使用复杂类型的属性呢,答案是会出现临时转换。
var n = 1;
n.toString()
// 会出现临时对象,得到结果后再回收
temp = new String(n)
temp.toString()
String
同样的两种声明方式
① var a = 'asdf';
② var b = new String('adsf');
一句话概括就是内存不同。
① 是在stack栈内存中直接储存。
② 是在stack栈内存中储存地址,然后在heap堆内存中存该地址的对象。
基础类型可以用复杂类型的属性,同上(Number)。
介绍几个String的api:
b.chart(x) --->表示的是b的第x字符
b.chart(0) === b[0]
.trim() ---->表示去除字符串两边的空格
" username ".trim()
// "username"
.concat() ----->连接两个字符串
var a = 'hello'
var b = 'world'
a.concat(b)
// "helloworld"
.slice() -----> 切取字符串中的一部分
var a = 'helloworld'
a.slise(0,4)
// "hell" 包前不包后,只切0-3.或者直接切4位
.replace('a','b') -------> 把字符串‘a’换成‘b’
var n = 'hello'
n.replace('h','w')
// "wello"
console.log(n) // "hello" n不变
Boolean
也是两种的声明方式:
var a = true
var b = new Boolean(true)
记住我5个falsy值:NaN,'',0,null,undefined
例:
var a = false
var b = new Boolean(false)
if(a){
console.log(1)
}if(b){
console.log(2)
}
// 2
Object
同样的两种声明方式:
var a = {}
var b = new Object()
a === b
// false (两个对象在stack栈内存中的地址不一样)
Object.prototype:对象的原型,也可叫共有属性。
var a = {}
a.__proto__ === Object.prototype
// true
var b = new Number(1)
b.__proto__ === Number.prototype //true
b.__proto__.__proto__ === Object.prototype //true
var c = new String('1')
c.__proto__ === String.prototype //true
c.__proto__.__proto__ === Object.prototype //true
var d = new Boolean(true)
d.__proto__ === Boolean.prototype //true
d.__proto__.__proto__ === Object.prototype //true
__ proto __ 与prototype的关系
对象.__ proto __ === 函数.prototype
二者的区别:__ proto __是对象的属性。prototype是函数的属性
var 对象 = new 函数()
对象.__ proto__ === 对象的构造函数.prototype
推论:
var number = new Number()
number.__proto__ === Number.prototype
Number.__proto__ === Function.prototype
// 因为Number是Function的实例
var object = new Object()
object.__proto__ === Object.prototype
Object.__proto__ === Function.prototype
//因为Object是Function的实例
var function = new Function()
function.__proto__ === Function.prototype
Function.__proto__ === Function.prototype
//因为Function是Function的实例