数据类型
基本类型
- String: 任意字符串
- Number: 任意的数字
- boolean: true/false
- undefined: undefined
- null: null
对象类型
- Object: 任意对象
- Function 一种特别的对象(可以执行)
- Array: 一种特别的对象
判断
- typeof //不能区分数组与对象、null与object
- instanceof //可以区分对象与数组
- ===
// 1、typeof 返回数据类型的字符串表达
var a;
console.log(typeof a==='undefined',typeof a === undefined) //true ,false
// 2、对象
var b1={
b2:[1,'abc',console.log],
b3: function()
{
console.log('b3')
}
}
console.log(b1 instanceof Object, b1 instanceof Array) // true false
console.log(b1.b2 instanceof Array,b1.b2 instanceof Object) // true true
问题 var a = xxx , a内存中保存的到底是什么
1、xxx 是基本数据,保存的就是这个数据
2、xxx 是对象, 保存的是对象的地址值
3、 xxx 是一个变量, 保存的xxx的内存内容(可能是基本数据,也可能是地址)
// obj1 obj2 指向同一个对象
var obj1 = {name: 'Tom'}
var obj2 = obj1
obj2.age =12
console.log(obj1.age,obj1.name) //12
function fn(obj)
{
obj.name = 'A'
}
fn(obj1)
console.log(obj2.name) //A
var a = {age: 12}
var b = a
a = {name: 'BOB', age:13}
b.age=14
console.log(b.age,a.name,a.age) // 14 BOB 13
function fn2(obj)
{
obj = {age: 15} //obj 指向了一个新的对象,并未改变原来对象的值
}
fn2(a)
console.log(a.age) // 13
问题 js调用函数是传递变量参数时,是值传递还是引用传递
理解1: 都是值(基本/地址值)传递
理解2:可能是值传递,也可能是引用传递(地址值)
var a = 3;
function fn(a)
{
a = a+1
}
fn(a);
console.log(a) //3
问题 JS引擎如何管理内存
1、内存生命周期
- 分配小内存空间,得到它的使用权
- 存储数据,可以反复进行操作
- 释放小内存空间
2、释放内存
- 局部变量: 函数执行完自动释放
- 对象: 成为垃圾对象 ==》 垃圾回收器回收
问题 什么时候必须使用[‘属性名’]的方式?
1、属性名包含特殊字符: - 空格
2、属性名不确定
var p = {}
// 1、 给p对象添加一个属性:content-type: text/json
// p.content-type = 'text/json' 不能用
p['content-type'] = 'text/json'
console.log(p['content-type'])
// 2、属性名不确定
var proName = 'myAge'
var value = 18
// p.proName = value 不能用
p[proName] = value
console.log(p[proName])