{"action":"created","issue":{"url":"https://api.github.com/repos/S-iscoming/myline_node/issues/8","repository_url":"https://api.github.com/repos/S-iscoming/myline_node","labels_url":"https://api.github.com/repos/S-iscoming/myline_node/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/S-iscoming/myline_node/issues/8/comments","events_url":"https://api.github.com/repos/S-iscoming/myline_node/issues/8/events","html_url":"https://github.com/S-iscoming/myline_node/issues/8","id":177951134,"number":8,"title":"EXM1","user":{"login":"S-iscoming","id":6531276,"avatar_url":"https://avatars.githubusercontent.com/u/6531276?v=3","gravatar_id":"","url":"https://api.github.com/users/S-iscoming","html_url":"https://github.com/S-iscoming","followers_url":"https://api.github.com/users/S-iscoming/followers","following_url":"https://api.github.com/users/S-iscoming/following{/other_user}","gists_url":"https://api.github.com/users/S-iscoming/gists{/gist_id}","starred_url":"https://api.github.com/users/S-iscoming/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/S-iscoming/subscriptions","organizations_url":"https://api.github.com/users/S-iscoming/orgs","repos_url":"https://api.github.com/users/S-iscoming/repos","events_url":"https://api.github.com/users/S-iscoming/events{/privacy}","received_events_url":"https://api.github.com/users/S-iscoming/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-09-20T02:59:31Z","updated_at":"2016-09-21T05:36:35Z","closed_at":null,"body":"## 问题一\r\n```javascript\r\nvar x = 1,\r\n  y = 2;\r\nvar z = function() {\r\n  var x = 2;\r\n  return {\r\n    x: x,\r\n    y: function(a, b) {\r\n      x = a + b;\r\n    },\r\n    z: function() {\r\n      return x;\r\n    }\r\n\r\n  }\r\n}\r\na = z();\r\na.y(x, y);\r\nalert(a.z());//3\r\nalert(a.x);//2\r\nalert(x);//1\r\n```\r\n## 背景\r\n在函数执行时,this 关键字并不会指向正在运行的函数本身,而是指向调用该函数的对象.所以,如果你想在函数内部获取函数自身的引用,只能使用函数名或者使用arguments.callee属性(严格模式下不可用),如果该函数是一个匿名函数,则你只能使用后者.\r\n例如：\r\n```javascript\r\n<script>\r\n        var obj2 = {\r\n            name:\"ssss\"\r\n        }\r\n        console.log(window.obj2);//Object {name: \"sjr\"}\r\n </script>\r\n(function(){\r\n        var obj = {//局部变量，只能在function中被引用，并不会成为谁的属性\r\n            name:\"ssss\"\r\n        }\r\n        console.log(this);//window（this指的是，调用函数的那个对象）\r\n        console.log(this.obj);//undefined\r\n    })()\r\n```\r\n## 再把闭包过一遍\r\n[学习Javascript闭包（Closure）](http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html)\r\n\r\n### 最基本的闭包：\r\n```javascript\r\nfunction f1(){\r\n　　　　var n=999;\r\n　　　　function f2(){\r\n　　　　　　alert(n); \r\n　　　　}\r\n　　　　return f2;\r\n　　}\r\n　　var result=f1();\r\n　　result(); // 999\r\n```\r\n### 加个setter\r\n```javascript\r\nfunction f1(){\r\n　　　　var n=999;\r\n　　　　nAdd=function(){n+=1}//注意\r\n　　　　function f2(){\r\n　　　　　　alert(n);\r\n　　　　}\r\n　　　　return f2;\r\n　　}\r\n　　var result=f1();\r\n　　result(); // 999\r\n　　nAdd();\r\n　　result(); // 1000\r\n```\r\n为什么会这样呢？原因就在于f1是f2的父函数，而f2被赋给了一个全局变量，这导致f2始终在内存中，而f2的存在依赖于f1，因此f1也始终在内存中，不会在调用结束后，被垃圾回收机制（garbage collection）回收。\r\n\"nAdd=function(){n+=1}\"这一行，首先在nAdd前面没有使用var关键字，因此nAdd是一个全局变量，而不是局部变量。其次，nAdd的值是一个匿名函数（anonymous function），而**这个匿名函数本身也是一个闭包**，所以nAdd相当于是一个setter，可以在函数外部对函数内部的局部变量进行操作。\r\n我的理解：\r\nvar result=f1();执行。retrn了一个f2。f2里用到了n，n是在f1里的，所以这个**n相对于f1**是一直存在的。\r\n### 阮的例子：\r\n```javascript\r\nvar name = \"The Window\";　　\r\nvar object = {　　　　\r\n  name: \"My Object\",\r\n  getNameFunc: function() {　　　　　　\r\n    return function() {　　　　　　　　\r\n      return this.name;　　　　　　\r\n    };　　　　\r\n  }　　\r\n};　　\r\nalert(object.getNameFunc()()); //\"The Window\"\r\n```\r\ngetNameFunc里面的函数有没有引用getNameFunc里的变量呢？\r\n**结合第二题**知道，并没有，这里的this是window。这个闭包应该是相对于window的，或者说它就不是个闭包。\r\n\r\n### 第二个例子：\r\n```javascript\r\nvar name = \"The Window\";\r\n　　var object = {\r\n　　　　name : \"My Object\",\r\n　　　　getNameFunc : function(){\r\n　　　　　　var that = this;\r\n　　　　　　return function(){\r\n　　　　　　　　return that.name;\r\n　　　　　　};\r\n　　　　}\r\n　　};\r\n　　alert(object.getNameFunc()());\r\n```\r\n这个例子getNameFunc里面的函数是**真的有**引用getNameFunc里的变量啦，所以你懂得。\r\n### 再来个自己写的例子：\r\n```javascript\r\nfunction fn1(n){\r\nfunction fn2(){\r\n  \tn++;\r\n    return n;\r\n  }\r\n  return fn2;\r\n}\r\n\r\nvar a = fn1(1);\r\nconsole.log(a());\r\n\r\nvar b = fn1(3);\r\nconsole.log(b());\r\n```\r\n可以看出a,b没什么关系，分别保有了各自的n。\r\n\r\n### [闭包的应用](http://www.cnblogs.com/darr/p/5085918.html)\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/S-iscoming/myline_node/issues/comments/248516304","html_url":"https://github.com/S-iscoming/myline_node/issues/8#issuecomment-248516304","issue_url":"https://api.github.com/repos/S-iscoming/myline_node/issues/8","id":248516304,"user":{"login":"S-iscoming","id":6531276,"avatar_url":"https://avatars.githubusercontent.com/u/6531276?v=3","gravatar_id":"","url":"https://api.github.com/users/S-iscoming","html_url":"https://github.com/S-iscoming","followers_url":"https://api.github.com/users/S-iscoming/followers","following_url":"https://api.github.com/users/S-iscoming/following{/other_user}","gists_url":"https://api.github.com/users/S-iscoming/gists{/gist_id}","starred_url":"https://api.github.com/users/S-iscoming/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/S-iscoming/subscriptions","organizations_url":"https://api.github.com/users/S-iscoming/orgs","repos_url":"https://api.github.com/users/S-iscoming/repos","events_url":"https://api.github.com/users/S-iscoming/events{/privacy}","received_events_url":"https://api.github.com/users/S-iscoming/received_events","type":"User","site_admin":false},"created_at":"2016-09-21T05:36:35Z","updated_at":"2016-09-21T05:36:35Z","body":"## 问题六\r\njs如何定义对象和继承一个对象（请写一个例子）\r\n"}}
{"actor":{"display_login":"S-iscoming"}}