diff --git "a/\346\211\213\345\206\231\344\270\200\344\270\252bind.js" "b/\346\211\213\345\206\231\344\270\200\344\270\252bind.js" index 82dad3e..34c161d 100644 --- "a/\346\211\213\345\206\231\344\270\200\344\270\252bind.js" +++ "b/\346\211\213\345\206\231\344\270\200\344\270\252bind.js" @@ -11,3 +11,33 @@ Function.prototype.myBind = function(context) { } //如果有构造函数 +function Person() { + this.name = 'p' + console.log(this) +} +// var a = Person.bind({va:1}) +var p = Person.call({ccc:'call'}) + +Function.prototype.myBind2 = function(ctx, ...args) { + const _this = this + const Temp = function() {} + const fn = function(...args2) { + return _this.apply(this instanceof fn ? this: ctx, [...args, ...args2]) + } + // fn.prototype = _this.prototype 这样当我们直接修改fn.prototype的时候也会修改绑定函数的prototype + + Temp.prototype = _this.prototype + fn.prototype = new Temp() + return fn +} + +function Person() { + this.name = 'p' + console.log(this) +} + +Person.prototype.pp ='pp' +var a = Person.myBind2({ccc:1}) +console.log(new a()) + +