call tostring this 克隆函数

区别对象 数组的三种方式

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];
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值