js变量、作用域

// 基本类型:4 'str' true/false undefined null       变量不能修改    不能添加属性
// 引用类型:[] {}                                                 变量可以修改

instanceof检测类型只能引用类型,不能基本类型consloe.log([] instanceof Array) consloe.log({} instanceof Object)     

// instanceof
// console.log([] instanceof Array);
// console.log([] instanceof Object);
// console.log({} instanceof Object);
// console.log({} instanceof Array);

// 作用域链
// var name = 'xm';
// function fn() {
// var name = 'xh';
// var sex = 'male';
// function fn2() {
// var name = 'xhei';
// var age = 18;
// }
// }

// 延长作用域链
// var person = {};
// person.name = 'xm';
// person.sex = 'male';
// var score = 4;

// with(person) {      //不推荐使用with
// name = 'xh';
// sex = 'female';
// score = 44;
// }
// console.log(person.name);
// console.log(person.sex);
// console.log(score);

垃圾收集机制

//垃圾收集机制:释放无用的数据,回收内存
//自动
//JS
//手动
//Objective-C
//原理:找出没用数据,打上标记,释放其内存;周期性执行
//标识无用数据的策略
//标记清除
//环境中的变量
//引用计数
var xm = {
name: 'xm',
age: 18
}; // 1
var xh = xm; // 2
xh = {}; // 1
xm = {}; // 0

//网景Netscape
//循环引用
function fn(argument) {
var xm = {}; // 1
var xh = {}; // 1
}
fn();
xm = null; // 0
xh = null; // 0

function fn(argument) {
var xm = {}; // 1
var xh = {}; // 1
xm.wife = xh; // 2
xh.husband = xm; // 2
}
fn();
xm = null; // 1
xh = null; // 1

//IE
//JS:DOM和BOM
//C++ COM

var obj = {};
var elem = document.getElementById('box'); // DOM
elem.someAttr = obj;
obj.someProperty = elem;

elem.someAttr = null;
obj.someProperty = null;

//内存管理
//Web浏览器 < 桌面应用程序
//null
var arr = [...];
//...
//... // arr
arr = null;
//...
//...
//...

猜你喜欢

转载自www.cnblogs.com/jian1234/p/10135349.html