{"action":"opened","issue":{"url":"https://api.github.com/repos/IdealVillage/js-FP/issues/1","repository_url":"https://api.github.com/repos/IdealVillage/js-FP","labels_url":"https://api.github.com/repos/IdealVillage/js-FP/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/IdealVillage/js-FP/issues/1/comments","events_url":"https://api.github.com/repos/IdealVillage/js-FP/issues/1/events","html_url":"https://github.com/IdealVillage/js-FP/issues/1","id":276808080,"number":1,"title":"call，apply，bind，new的模拟实现","user":{"login":"IdealVillage","id":28339036,"avatar_url":"https://avatars1.githubusercontent.com/u/28339036?v=4","gravatar_id":"","url":"https://api.github.com/users/IdealVillage","html_url":"https://github.com/IdealVillage","followers_url":"https://api.github.com/users/IdealVillage/followers","following_url":"https://api.github.com/users/IdealVillage/following{/other_user}","gists_url":"https://api.github.com/users/IdealVillage/gists{/gist_id}","starred_url":"https://api.github.com/users/IdealVillage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IdealVillage/subscriptions","organizations_url":"https://api.github.com/users/IdealVillage/orgs","repos_url":"https://api.github.com/users/IdealVillage/repos","events_url":"https://api.github.com/users/IdealVillage/events{/privacy}","received_events_url":"https://api.github.com/users/IdealVillage/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-11-26T08:16:39Z","updated_at":"2017-11-26T08:16:39Z","closed_at":null,"author_association":"OWNER","body":"本文是看完[冴羽大大](https://github.com/mqyqingfeng/Blog)的博客后的笔记\r\n`//使用一个指定的this值和若干个指定参数的情况下调用某个函数和某个方法\r\n//call的实现:改变this指向，指向绑定的对象,执行函数\r\n\r\n    Function.prototype.call2=function (context) {\r\n        context=context||window;\r\n        //this:绑定的函数\r\n        context.fn=this;\r\n    var args=[];\r\n    for(var i=1;i<arguments.length;i++){\r\n        //从1开始，也就是从要绑定的对象之后开始视作传入参数\r\n        args.push('arguments['+i+']');\r\n    }\r\n    var result=eval('context.fn('+args+')');\r\n    //为对象创建了一个方法，为方法传入参数执行完毕后，删除这个方法\r\n    delete context.fn;\r\n    return result;\r\n};\r\n\r\nvar foo={\r\n    a:2\r\n};\r\n\r\nvar bar=function () {\r\n    console.log(this.a);\r\n};\r\n\r\nbar.call2(foo);\r\n\r\n//apply的实现，跟call的区别之处,传入参数为数组形式\r\nFunction.prototype.apply2=function (context,arr) {\r\n    context=Object(context)||window;\r\n    context.fn=this;\r\n    var result;\r\n    //传入的不是对象，视作无对象直接执行函数\r\n    if(!arr){\r\n        result=context.fn();\r\n    }else {\r\n        var args=[];\r\n        for(var i=1,len=arguments.length;i<len;i++){\r\n            args.push('arguments['+i+']');\r\n        }\r\n        result=eval('context.fn('+args+')');\r\n    }\r\n    delete context.fn;\r\n    return result;\r\n};\r\n\r\nFunction.prototype.bind2=function (context) {\r\n    if (typeof this !== \"function\") {\r\n        throw new Error(\"Function.prototype.bind - what is trying to be bound is not callable\");\r\n    }\r\n    var self=this;\r\n    var args=[].slice.call(arguments,1);\r\n    //中间对象fNop,这样继承构造函数的对象修改它的原型不会把self的原型一起修改\r\n    var fNop=function () {};\r\n    var fBound=function () {\r\n        var bindArgs=[].slice.call(arguments);\r\n        //若将返回的函数作为构造函数新建一个实例,则将this绑定到这个新建的对象上。如果作为普通函数，则正常传参\r\n        return self.apply(this instanceof fBound?this:context,args.concat(bindArgs));\r\n    };\r\n    fNop.prototype=self.prototype;\r\n    fBound.prototype=new fNop();\r\n    return fBound;\r\n};\r\n\r\nfunction objectFactory() {\r\n    var obj=new Object(),\r\n        constructor=[].shift.call(arguments);\r\n    //指向构造函数的原型\r\n    obj.__proto__=constructor.prototype;\r\n    //将构造函数的this指向新建对象，这样对象可以访问构造函数上的属性\r\n    var ret=constructor.apply(obj,arguments);\r\n    //如果构造返回值是一个对象，则返回这个对象，如果不是对象，改返回什么就返回什么\r\n    return typeof ret==='object'?ret:obj;\r\n}\r\n`"}}
{"actor":{"display_login":"IdealVillage"}}