{"id":27995562,"url":"https://github.com/chokcoco/apply-call-bind","last_synced_at":"2026-03-08T13:39:29.613Z","repository":{"id":95026057,"uuid":"43048744","full_name":"chokcoco/apply-call-bind","owner":"chokcoco","description":"深入浅出妙用Javascript中apply、call、bind","archived":false,"fork":false,"pushed_at":"2017-08-11T07:45:48.000Z","size":140,"stargazers_count":64,"open_issues_count":3,"forks_count":33,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-08T20:44:03.904Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chokcoco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-09-24T06:22:38.000Z","updated_at":"2024-03-14T14:27:28.000Z","dependencies_parsed_at":"2023-04-04T01:18:17.241Z","dependency_job_id":null,"html_url":"https://github.com/chokcoco/apply-call-bind","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chokcoco/apply-call-bind","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chokcoco%2Fapply-call-bind","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chokcoco%2Fapply-call-bind/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chokcoco%2Fapply-call-bind/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chokcoco%2Fapply-call-bind/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chokcoco","download_url":"https://codeload.github.com/chokcoco/apply-call-bind/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chokcoco%2Fapply-call-bind/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30258532,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-08T08:59:44.879Z","status":"ssl_error","status_checked_at":"2026-03-08T08:58:02.867Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-08T20:33:27.671Z","updated_at":"2026-03-08T13:39:29.595Z","avatar_url":"https://github.com/chokcoco.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 深入浅出 妙用Javascript中apply、call、bind\n\n## apply、call用法的简单示例\n```javascript\nfunction fruits() {}\n\nfruits.prototype = {\n\tcolor: \"red\",\n\tsay: function() {\n\t\tconsole.log(\"My color is \" + this.color);\n\t}\n}\n\nvar apple = new fruits;\napple.say();\t//My color is red\n\nbanana = {\n\tcolor: \"yellow\"\n}\napple.say.call(banana);\t\t//My color is yellow\napple.say.apply(banana);\t//My color is yellow\n```\n\n\n## apply 、 call 区别\n注意apply传递的参数是数组，而call是按参数顺序传递\n```javascript\nvar func = function(arg1, arg2) {\n\n};\n\nfunc.call(this, arg1, arg2); \nfunc.apply(this, [arg1, arg2])\n```\n\n## apply 、 call 用法示例\n\n* 数组之间追加\n```javascript\nvar array1 = [12 , \"foo\" , {name \"Joe\"} , -2458];  \nvar array2 = [\"Doe\" , 555 , 100];  \nArray.prototype.push.apply(array1, array2);  \n/* array1 值为  [12 , \"foo\" , {name \"Joe\"} , -2458 , \"Doe\" , 555 , 100] */\n```\n\n* 获取数组中的最大值和最小值\n```javascript\nvar  numbers = [5, 458 , 120 , -215 ];  \nvar maxInNumbers = Math.max.apply(Math, numbers),\t//458\n\tmaxInNumbers = Math.max.call(Math,5, 458 , 120 , -215);\t//458\n```\n\n* 验证是否是数组（前提是toString()方法没有被重写过）\n```javascript\nfunctionisArray(obj){  \n    returnObject.prototype.toString.call(obj) === '[object Array]' ;\n}\n```\n\n* 类（伪）数组使用数组方法\n```javascript\nvar domNodes = Array.prototype.slice.call(document.getElementsByTagName(\"*\"));\n```\n\n## 一道面试题目\n```javascript\n//使用 log 代理 console.log\nfunction log(msg)　{\n  console.log(msg);\n}\n\nlog(1);\nlog(1,2);\n\n//优雅的方法\nfunction log(){\n  console.log.apply(console, arguments);\n};\n\nlog(1);\nlog(1,2);\n\n//添加一个 (app) 前缀\nfunction log(){\n  var args = Array.prototype.slice.call(arguments);\n  args.unshift('(app)');\n\n  console.log.apply(console, args);\n};\n```\n\n## bind 用法简单示例\n```javascript\n// 正常情况下使用变量保存 this 值\nvar foo = {\n\tbar : 1,\n\teventBind: function(){\n\t\tvar _this = this ;\n\t\t$('.someClass').on('click',function(event) {\n\t\t\t/* Act on the event */\n\t\t\tconsole.log(_this.bar);\t\t//1\n\t\t});\n\t}\n}\n\n// 使用 bind 进行函数绑定\nvar foo = {\n\tbar : 1,\n\teventBind: function(){\n\t\t$('.someClass').on('click',function(event) {\n\t\t\t/* Act on the event */\n\t\t\tconsole.log(this.bar);\t\t//1\n\t\t}.bind(this));\n\t}\n}\n```\n\n```javascript\nvarfoo = {\n    x: 3\n}\n \nvar bar = function(){\n    console.log(this.x);\n}\n \nbar(); // undefined\nvar func = bar.bind(foo);\nfunc(); // 3\n```\n\n## apply、call、bind 比较\n``` javascript\nvar obj = {\n\tx: 81,\n};\n\nvar foo = {\n\tgetX: function() {\n\t\treturn this.x;\n\t}\n}\n\nconsole.log(foo.getX.bind(obj)());\t\t//81\nconsole.log(foo.getX.call(obj));\t\t//81\nconsole.log(foo.getX.apply(obj));\t\t//81\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchokcoco%2Fapply-call-bind","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchokcoco%2Fapply-call-bind","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchokcoco%2Fapply-call-bind/lists"}