区别对象 数组的三种方式
1、construcctor属性
2、instanceof()方法
3、toString方法
this
1.函数预编译过程 this —> window
分析 :
首先 预编译过程创建AO 执行期上下文
此时this指向window
当时如果构造函数一但new 一个对象 就会引发隐式创建var this 三部曲 如图所示 此时改变this指向
2.全局作用域里 this —> window
3.call/apply 可以改变函数运行时this指向
4.obj.func(); func()里面的this指向obj
特别注意 arguments。callee 指向函数自身的引用 适用于立即执行函数 和递归的情景
深度克隆函数
var obj = {
name: 123,
age: 'ttt',
max: [1, 2, 3, 4],
score: {
a: 555,
b: 444
}
}
var obj1 = {};
function clone(origin, target) {
var target = target || {},
tostr = Object.prototype.toString,
arr = '[object Array]';
for (prop in origin) {
if (origin.hasOwnProperty(prop)) {
if (origin[prop] !== 'null' && typeof(origin[prop]) == 'object') {
if (tostr.call(origin[prop]) == arr) {
target[prop] = [];
} else {
target[prop] = {};
}
clone(origin[prop], target[prop]);
} else {
target[prop] = origin[prop];
}
}
}
}