{"action":"opened","issue":{"url":"https://api.github.com/repos/shangwenmo/shangwenmo.github.io/issues/12","labels_url":"https://api.github.com/repos/shangwenmo/shangwenmo.github.io/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/shangwenmo/shangwenmo.github.io/issues/12/comments","events_url":"https://api.github.com/repos/shangwenmo/shangwenmo.github.io/issues/12/events","html_url":"https://github.com/shangwenmo/shangwenmo.github.io/issues/12","id":102533175,"number":12,"title":"JavaScript arguments","user":{"login":"shangwenmo","id":7699125,"avatar_url":"https://avatars.githubusercontent.com/u/7699125?v=3","gravatar_id":"","url":"https://api.github.com/users/shangwenmo","html_url":"https://github.com/shangwenmo","followers_url":"https://api.github.com/users/shangwenmo/followers","following_url":"https://api.github.com/users/shangwenmo/following{/other_user}","gists_url":"https://api.github.com/users/shangwenmo/gists{/gist_id}","starred_url":"https://api.github.com/users/shangwenmo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shangwenmo/subscriptions","organizations_url":"https://api.github.com/users/shangwenmo/orgs","repos_url":"https://api.github.com/users/shangwenmo/repos","events_url":"https://api.github.com/users/shangwenmo/events{/privacy}","received_events_url":"https://api.github.com/users/shangwenmo/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/shangwenmo/shangwenmo.github.io/labels/JavaScript","name":"JavaScript","color":"fef2c0"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-22T12:46:33Z","updated_at":"2015-08-22T12:46:33Z","closed_at":null,"body":"### arguments\r\n* JavaScript 中每个函数内都能访问一个特别变量 **arguments**。这个变量维护着所有传递到这个函数中的参数列表。\r\n\r\n* **arguments** 变量不是一个数组，实际上它是一个对象。因此，无法对 **arguments** 变量使用标准的数组方法，比如 push, pop 或者 slice。\r\n\r\n* 转化为数组\r\n\r\n        Array.prototype.slice.call(arguments);\r\n\r\n#### 举个例子\r\n* CodeWars 上的一道题，题目是这样的：\r\n\r\n        var lazy_value = make_lazy(add, 1, 2);\r\n\r\n        function add(a, b) {\r\n          return a+b;\r\n        }\r\n\r\n        // The expression does not get evaluated at the moment,\r\n        // but only when you invoke lazy_value as:\r\n        lazy_value(); // 5\r\n\r\n        function make_lazy() {\r\n          ......\r\n        }\r\n\r\n* 我写的方法\r\n\r\n        var make_lazy = function () {\r\n          var args = Array.prototype.slice.call(arguments);\r\n          var method = args.shift();\r\n            return function() {\r\n              return method.apply(null, args);\r\n            };\r\n        }；\r\n\r\n* 高票方法之一\r\n\r\n        function make_lazy(fn) {\r\n            var args = [].slice.call(arguments, 1)\r\n            return function() {\r\n              return fn.apply(this, args);\r\n            };\r\n          }；\r\n\r\n    * **fn** 作为参数，指向内存中的首地址，也就是方法 **add()**。\r\n\r\n* 高票方法之二\r\n\r\n        var make_lazy = function (fn) {\r\n          return fn.bind.apply(fn, arguments);\r\n        };\r\n\r\n    * fun.bind(scope, arg1, arg2, ... )，返回一个新的 **fun** 对象，作用域在 **scope** 下；\r\n\r\n    * fun.apply(scope, argArray)，当 **apply** 被一个方法调用时，在给定的 **scope** 下执行方法 **fun**，并将参数 **argArray** 传给方法，所以下面的两个表达式是等价的\r\n\r\n            fun.apply(scope, [1, 2, 3]);\r\n            scope.fun(1, 2, 3); // assuming `fun` exists on the scope object\r\n\r\n    * 分解上面的表达式\r\n\r\n            fn.bind.apply(fn, arguments); ==>\r\n\r\n            fn.bind.apply(fn, [fn, 1, 2); ==>\r\n\r\n            fn.bind(fn, 1, 2);\r\n\r\n****\r\n### 参考\r\n* MDN\r\n* 《JavaScript 秘密花园》\r\n"}}