{"id":8763,"date":"2023-05-22T21:20:34","date_gmt":"2023-05-22T13:20:34","guid":{"rendered":"\/?p=8763"},"modified":"2023-08-23T00:37:18","modified_gmt":"2023-08-22T16:37:18","slug":"js-%e5%af%b9%e8%b1%a1%e7%bb%a7%e6%89%bf%ef%bc%8c%e5%8e%9f%e5%9e%8b%e9%93%bethis","status":"publish","type":"post","link":"\/?p=8763","title":{"rendered":"js-\u5bf9\u8c61,\u7ee7\u627f\uff0c\u539f\u578b\u94fe,this"},"content":{"rendered":"<h2>1.JS\u57fa\u7840-\u5bf9\u8c61Object<\/h2>\n<p>\u901a\u8fc7 JavaScript\uff0c\u60a8\u80fd\u591f\u5b9a\u4e49\u5e76\u521b\u5efa\u81ea\u5df1\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u521b\u5efa\u65b0\u5bf9\u8c61\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 Object \u5b9a\u4e49\u5e76\u521b\u5efa\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u4f7f\u7528\u51fd\u6570\u6765\u5b9a\u4e49\u5bf9\u8c61\uff0c\u7136\u540e\u521b\u5efa\u65b0\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<p>\u5728 JavaScript \u4e2d\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u662f Object \u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5b83\u4eec\u90fd\u4f1a\u4ece Object.prototype \u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>Object \u6784\u9020\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5305\u88c5\u5668\u3002<\/p>\n<p>Object \u6784\u9020\u51fd\u6570\uff0c\u4f1a\u6839\u636e\u7ed9\u5b9a\u7684\u53c2\u6570\u521b\u5efa\u5bf9\u8c61\uff0c\u5177\u4f53\u6709\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u7ed9\u5b9a\u503c\u662f null \u6216 undefined\uff0c\u5c06\u4f1a\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u3002<\/li>\n<li>\u5982\u679c\u4f20\u8fdb\u53bb\u7684\u662f\u4e00\u4e2a\u57fa\u672c\u7c7b\u578b\u7684\u503c\uff0c\u5219\u4f1a\u6784\u9020\u5176\u5305\u88c5\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u5982\u679c\u4f20\u8fdb\u53bb\u7684\u662f\u5f15\u7528\u7c7b\u578b\u7684\u503c\uff0c\u4ecd\u7136\u4f1a\u8fd4\u56de\u8fd9\u4e2a\u503c\uff0c\u7ecf\u4ed6\u4eec\u590d\u5236\u7684\u53d8\u91cf\u4fdd\u6709\u548c\u6e90\u5bf9\u8c61\u76f8\u540c\u7684\u5f15\u7528\u5730\u5740\u3002<\/li>\n<li>\u5f53\u4ee5\u975e\u6784\u9020\u51fd\u6570\u5f62\u5f0f\u88ab\u8c03\u7528\u65f6\uff0cObject \u7684\u884c\u4e3a\u7b49\u540c\u4e8e new Object()\u3002<\/li>\n<\/ul>\n<h3>1.1\u521b\u5efa\u5bf9\u8c61\u7684\u65b9\u6cd5<\/h3>\n<ol>\n<li>\n<p>new Objcet<\/p>\n<pre><code class=\"language-javascript\">person=new Object();\nperson.firstname=\"John\";\nperson.lastname=\"Doe\";\nperson.age=50;\nperson.eyecolor=\"blue\";<\/code><\/pre>\n<\/li>\n<li>\n<p>\u952e\u503c\u5bf9<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u540c\u4e0a\uff0c\u4e5f\u662f\u4f7f\u7528 Object\u6765\u521b\u5efa\u5bf9\u8c61\n\/\/ JavaScript \u5bf9\u8c61\u5c31\u662f\u4e00\u4e2a name:value \u96c6\u5408\nperson={firstname:\"John\",lastname:\"Doe\",age:50,eyecolor:\"blue\"};<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5bf9\u8c61\u6784\u9020\u5668<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u672c\u4f8b\u4f7f\u7528\u51fd\u6570\u6765\u6784\u9020\u5bf9\u8c61\nfunction person(firstname,lastname,age,eyecolor)\n{\n   this.firstname=firstname;\n   this.lastname=lastname;\n   this.age=age;\n   this.eyecolor=eyecolor;\n}<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>1.2 \u628a\u65b9\u6cd5\u6dfb\u52a0\u5230 JavaScript \u5bf9\u8c61<\/h3>\n<p>\u65b9\u6cd5\u53ea\u4e0d\u8fc7\u662f\u9644\u52a0\u5728\u5bf9\u8c61\u4e0a\u7684\u51fd\u6570\u3002<\/p>\n<p>\u5728\u6784\u9020\u5668\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u5bf9\u8c61\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-javascript\">function person(firstname,lastname,age,eyecolor)\n{\n    this.firstname=firstname;\n    this.lastname=lastname;\n    this.age=age;\n    this.eyecolor=eyecolor;\n\n    this.changeName=changeName;\n    function changeName(name)\n    {\n        this.lastname=name;\n    }\n}<\/code><\/pre>\n<h3>1.3 JavaScript \u7c7b<\/h3>\n<p>JavaScript \u662f\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\uff0c\u4f46 JavaScript \u4e0d\u4f7f\u7528\u7c7b\u3002<\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u4e0d\u4f1a\u521b\u5efa\u7c7b\uff0c\u4e5f\u4e0d\u4f1a\u901a\u8fc7\u7c7b\u6765\u521b\u5efa\u5bf9\u8c61\uff08\u5c31\u50cf\u5728\u5176\u4ed6\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\u4e2d\u90a3\u6837\uff09\u3002<\/p>\n<p>JavaScript \u57fa\u4e8e prototype\uff0c\u800c\u4e0d\u662f\u57fa\u4e8e\u7c7b\u7684\u3002<\/p>\n<h2>2.JS\u57fa\u7840-\u539f\u578b\u94fe<\/h2>\n<h3>2.1 constructor \u7684\u542b\u4e49<\/h3>\n<p><strong><em>constructor \u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u548c\u521d\u59cb\u5316class\u521b\u5efa\u7684\u5bf9\u8c61\u7684\u7279\u6b8a\u65b9\u6cd5\u3002<\/em><\/strong><\/p>\n<p>\u5728\u4e00\u4e2a\u7c7b\u4e2d\u53ea\u80fd\u6709\u4e00\u4e2aconstructor\u65b9\u6cd5\uff0c\u672c\u8d28\u662f\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u4e0d\u6307\u5b9a\u4e00\u4e2a\u6784\u9020\u51fd\u6570(constructor)\u65b9\u6cd5, \u5219\u4f7f\u7528\u4e00\u4e2a\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570(constructor)\u3002<\/p>\n<p>\u6240\u8c13\u201c\u6784\u9020\u51fd\u6570\u201d\uff0c\u5c31\u662f\u4e13\u95e8\u7528\u6765\u751f\u6210\u5b9e\u4f8b\u5bf9\u8c61\u7684\u51fd\u6570\u3002\u5b83\u5c31\u662f\u5bf9\u8c61\u7684\u6a21\u677f\uff0c\u63cf\u8ff0\u5b9e\u4f8b\u5bf9\u8c61\u7684\u57fa\u672c\u7ed3\u6784\u3002<\/p>\n<ul>\n<li>\u4e00\u4e2a\u7c7b\u5fc5\u987b\u6709<code>constructor<\/code>\u65b9\u6cd5\uff0c\u5982\u679c\u6ca1\u6709\u663e\u793a\u5b9a\u4e49\uff0c\u4e00\u4e2a\u7a7a\u7684<code>constructor<\/code>\u65b9\u6cd5\u4f1a\u88ab\u9ed8\u8ba4\u6dfb\u52a0\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/* 1. ***************************************\/\n\/\/ \u5b9a\u4e49\u4e86\u4e00\u4e2a\u7a7a\u7684\u7c7bPoint\uff0c\nclass Point {\n    \/\/ JavaScript\u5f15\u64ce\u4f1a\u81ea\u52a8\u4e3a\u5b83\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u7684constructor\u65b9\u6cd5\n}\n\n\/\/ \u7b49\u540c\u4e8e\nclass Point {\n  constructor() {}\n}\n\/* 2.***************************************\/\n\/\/ \u9996\u5148\u58f0\u660e\u4e00\u4e2a People \u7c7b\nclass People{\n  constructor(sex,age) {\n    this.name = &#039;\u4e09\u4e0a\u60a0\u4e9a&#039;;\n    this.sex = sex;\n    this.age = age;\n  }\n}\nconst people1= new People();\nconsole.log(people1.name); \/\/\u4e09\u4e0a\u60a0\u4e9a\npeople1.constructor \/\/ === People\n\/\/class People{\n\/\/  constructor(sex,age) {\n\/\/    this.name = &#039;\u4e09\u4e0a\u60a0\u4e9a&#039;;\n\/\/    this.sex = sex;\n\/\/    this.age = age;\n\/\/  }\n\/\/}\n\n\/* 3.***************************************\/\nvar txt = &quot;Hello World!&quot;;\ntxt.constructor  \/\/ \u0192 String() { [native code] }<\/code><\/pre>\n<ul>\n<li><code>constructor<\/code>\u5c5e\u6027\u8868\u793a\u539f\u578b\u5bf9\u8c61\u4e0e\u6784\u9020\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\uff0c\u5982\u679c\u4fee\u6539\u4e86\u539f\u578b\u5bf9\u8c61\uff0c\u4e00\u822c\u4f1a\u540c\u65f6\u4fee\u6539<code>constructor<\/code>\u5c5e\u6027\uff0c\u9632\u6b62\u5f15\u7528\u7684\u65f6\u5019\u51fa\u9519\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">function Person(name) {\n  this.name = name;\n}\n\nPerson.prototype.constructor === Person \/\/ true\n\nPerson.prototype = {\n  method: function () {}\n};\n\nPerson.prototype.constructor === Person \/\/ false\nPerson.prototype.constructor === Object \/\/ true<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230522211013275.png\" alt=\"image-20230522211013275\" style=\"zoom:50%;\" \/> <\/p>\n<p>\u5728JavaScript\u4e2d\uff0cprototype\u5bf9\u8c61\u662f\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u4e00\u4e2a\u91cd\u8981\u673a\u5236\u3002<\/p>\n<p>\u6bcf\u4e2a\u51fd\u6570\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\uff08Function\uff09\uff0c\u51fd\u6570\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5b50\u5bf9\u8c61 prototype\u5bf9\u8c61\uff0c\u7c7b\u662f\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u6765\u5b9a\u4e49\u7684\u3002prototype\u8868\u793a\u8be5\u51fd\u6570\u7684\u539f\u578b\uff0c\u4e5f\u8868\u793a\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u7684\u96c6\u5408\u3002<\/p>\n<p>\u5728\u901a\u8fc7new\u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\u7684\u65f6\u5019\uff0cprototype\u5bf9\u8c61\u7684\u6210\u5458\u90fd\u6210\u4e3a\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u6210\u5458\u3002<\/p>\n<p>1\u3001\u8be5\u5bf9\u8c61\u88ab\u7c7b\u6240\u5f15\u7528\uff0c\u53ea\u6709\u51fd\u6570\u5bf9\u8c61\u624d\u53ef\u5f15\u7528\uff1b<\/p>\n<p>2\u3001\u5728new\u5b9e\u4f8b\u5316\u540e\uff0c\u5176\u6210\u5458\u88ab\u5b9e\u4f8b\u5316\uff0c\u5b9e\u4f8b\u5bf9\u8c61\u65b9\u53ef\u8c03\u7528\u3002<\/p>\n<p>\u540c\u65f6\uff0c\u51fd\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u51fd\u6570\u5bf9\u8c61\u82e5\u76f4\u63a5\u58f0\u660e\u6210\u5458\uff0c\u4e0d\u7528\u88ab\u5b9e\u4f8b\u5316\u5373\u53ef\u8c03\u7528\u3002<\/p>\n<h3>2.2 \u539f\u578b\u5bf9\u8c61prototype<\/h3>\n<p>\u6211\u4eec\u9996\u5148\u603b\u7ed3\u4e00\u4e0b\u539f\u578b\u5bf9\u8c61\u7684\u4f5c\u7528\uff1a<\/p>\n<ol>\n<li>\u539f\u578b\u5bf9\u8c61\u5c31\u76f8\u5f53\u4e8e\u4e00\u4e2a\u516c\u5171\u7684\u533a\u57df\uff0c\u6240\u6709\u540c\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230\u8fd9\u4e2a\u539f\u578b\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5bf9\u8c61\u4e2d\u5171\u6709\u7684\u5185\u5bb9\uff0c\u7edf\u4e00\u8bbe\u7f6e\u5230\u539f\u578b\u5bf9\u8c61\u4e2d\u53bb\u3002<\/li>\n<li>\u5f53\u6211\u4eec\u8bbf\u95ee\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u65f6\uff0c\u5b83\u4f1a\u5148\u5728\u5bf9\u8c61\u81ea\u8eab\u4e2d\u5bfb\u627e\uff0c\u5982\u679c\u627e\u5230\u8981\u67e5\u627e\u7684\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u5c31\u4f1a\u76f4\u63a5\u4f7f\u7528\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u4f1a\u53bb\u539f\u578b\u5bf9\u8c61\u4e2d\u67e5\u627e\uff0c\u627e\u5230\u4e86\u5c31\u4f1a\u76f4\u63a5\u4f7f\u7528\u3002<\/li>\n<li>\u5728\u6211\u4eec\u521b\u5efa\u6784\u9020\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u5bf9\u8c61\u7684\u5171\u6709\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u7edf\u4e00\u6dfb\u52a0\u5230\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u5bf9\u8c61\u4e2d\u53bb\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5c31\u662f\u4e0d\u7528\u5206\u522b\u4e3a\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u6dfb\u52a0\u8fd9\u4e9b\u5171\u6709\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u540c\u65f6\u6bcf\u4e2a\u5bf9\u8c61\u4e5f\u90fd\u5177\u6709\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u53ef\u4ee5\u8bf4\u7ed9\u539f\u578b\u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\uff0c\u51fd\u6570\u7684\u6240\u6709\u5b9e\u4f8b\u5bf9\u8c61\u90fd\u4f1a\u81ea\u52a8\u62e5\u6709\u539f\u578b\u4e2d\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<ul>\n<li>\u666e\u904d\u6765\u8bf4\uff0c\u5bf9\u4e8e\u6240\u6709\u51fd\u6570\uff0c\u6bcf\u4e2a\u51fd\u6570\u662f\u90fd\u4f1a\u542b\u6709\u4e00\u4e2aprototype\u5c5e\u6027\uff0c\u5b83\u9ed8\u8ba4\u6307\u5411\u4e00\u4e2aobject\u7a7a\u5bf9\u8c61(\u4e5f\u79f0\u4e3a\u539f\u578b\u5bf9\u8c61)\u3002<\/li>\n<li>\u539f\u578b\u5bf9\u8c61\u4e2d\u90fd\u6709\u4e00\u4e2aconstructor\u5c5e\u6027,\u5b83\u662f\u6307\u5411\u51fd\u6570\u5bf9\u8c61\u7684<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">function fun(){\n    console.log(&#039;fun&#039;)\n    }\nconsole.log(fun.prototype) \/\/\u9ed8\u8ba4\u6307\u5411\u4e00\u4e2aObject\u7a7a\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u6ca1\u6709\u6211\u4eec\u7684\u5c5e\u6027\n\/\/\u539f\u578b\u5bf9\u8c61\u4e2d\u90fd\u6709\u4e00\u4e2aconstructor\u5c5e\u6027\uff0c\u5b83\u6307\u5411\u51fd\u6570\u5bf9\u8c61\nconsole.log(fun.prototype.constructor === fun) \/\/true<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230522173450959.png\" alt=\"image-20230522173450959\" \/> <\/p>\n<h3>2.3 prototype \u548c __proto__<\/h3>\n<ol>\n<li>\n<p>\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709prototype(\u663e\u5f0f\u539f\u578b)\u548c<code>__proto__<\/code>(\u9690\u5f0f\u539f\u578b);<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u4e2a\u5bf9\u8c61\/\u6784\u9020\u5668\u51fd\u6570\u7684\u5b9e\u4f8b\uff08\u8fd9\u4e2a\u4e5f\u662f\u5bf9\u8c61\uff09\u90fd\u6709<code>__proto__<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u4f8b\u7684<code>__proto__<\/code>\u6307\u5411\u6784\u9020\u51fd\u6570\u7684prototype\u3002\u8fd9\u4e2a\u79f0\u4e3a<strong><font color=red>\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u5bf9\u8c61<\/font><\/strong>\uff1b<\/p>\n<\/li>\n<li>\n<p>js\u5f15\u64ce\u4f1a\u6cbf\u7740<code>__proto__<\/code>-&gt;prototype\u7684\u987a\u5e8f\u4e00\u76f4\u5f80\u4e0a\u65b9\u67e5\u627e\uff0c\u627e\u5230window.Object.prototype\u4e3a\u6b62\uff0cObject\u4e3a\u539f\u751f\u5e95\u5c42\u5bf9\u8c61\uff0c\u5230\u8fd9\u91cc\u5c31\u505c\u6b62\u4e86\u67e5\u627e\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u4f1a\u62a5\u9519\u6216\u8005\u8fd4\u56deundefined\uff1b<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230423214325358.png\" alt=\"image-20230423214325358\" \/> <\/p>\n<\/li>\n<li>\n<p>\u800c\u6784\u9020\u51fd\u6570\u7684<code>__proto__<\/code>\u6307\u5411<code>Function.prototype f() {[native code]}<\/code>\u3010<strong><font color=red>\u6784\u9020\u5668\u51fd\u6570\uff0c\u4f46\u8fd9\u4e2a\u53eb\u6cd5\u5e76\u4e0d\u51c6\u786e\uff0c\u5b83\u76ee\u524d\u6ca1\u6709\u4e00\u4e2a\u5408\u9002\u7684\u4e2d\u6587\u540d<\/font><\/strong>\u3011\uff1b<\/p>\n<\/li>\n<li>\n<p>__proto__\u662f\u6d4f\u89c8\u5668\u5382\u5546\u5b9e\u73b0\u7684\uff0cW3C\u89c4\u8303\u4e2d\u5e76\u6ca1\u6709\u8fd9\u4e2a\u4e1c\u897f\uff1b<\/p>\n<\/li>\n<li>\n<p>\u6240\u6709\u5bf9\u8c61\u90fd\u5177\u6709__proto__\u662f\u8fc7\u53bb\u5177\u6709\uff0c\u672a\u6765\u5c06\u79fb\u9664\uff0c\u56e0\u4e3a\u89c4\u8303\u672a\u5b9a\u4e49\uff0c\u53ea\u662f\u6d4f\u89c8\u5668\u5b9e\u73b0\u7684\u4e00\u4e2aget\u5c5e\u6027\u3002<\/p>\n<\/li>\n<\/ol>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230423212957799.png\" alt=\"image-20230423212957799\" \/><\/p>\n<ol>\n<li>\n<p>JS\u4ee3\u7801\u8fd8\u6ca1\u8fd0\u884c\u7684 \u65f6\u5019\uff0cJS\u73af\u5883\u91cc\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2awindow\u5bf9\u8c61\u3002\u51fd\u6570\u662f\u5bf9\u8c61;<\/p>\n<\/li>\n<li>\n<p>window\u5bf9\u8c61\u6709\u4e00\u4e2aObject\u5c5e\u6027\uff0cwindow.Object\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61;<\/p>\n<\/li>\n<li>\n<p>window.Object\u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\u6709\u4e00\u4e2a\u91cd\u8981\u5c5e\u6027\u662fprototype;<\/p>\n<\/li>\n<li>\n<p>window.Object.prototype\u91cc\u9762\u6709\u4e00\u5806\u5c5e\u6027;<\/p>\n<\/li>\n<li>\n<p><font color='green'>\u6240\u6709\u7684\u5b9e\u4f8b\u51fd\u6570<code>__proto__<\/code>\u90fd\u4f1a\u6307\u5411\u6784\u9020\u51fd\u6570\u7684prototype<\/font>;<\/p>\n<\/li>\n<li>\n<p><font color='green'>constructor\u662f\u53cd\u5411\u7684prototype;<\/font><\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230423213622115.png\" alt=\"image-20230423213622115\" \/> <\/p>\n<\/li>\n<\/ol>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230423221241038.png\" alt=\"image-20230423221241038\" \/> <\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230423221435980.png\" alt=\"image-20230423221435980\" \/> <\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230423221508910.png\" alt=\"image-20230423221508910\" \/> <\/p>\n<p><strong>\u518d\u590d\u6742\u4e00\u4e0b\uff0carr.valueOf() \u505a\u4e86\u4ec0\u4e48\uff1f<\/strong><\/p>\n<ul>\n<li><code>arr<\/code> \u81ea\u8eab\u6ca1\u6709 valueOf\uff0c\u4e8e\u662f\u53bb <code>arr.__proto__<\/code> \u4e0a\u627e;<\/li>\n<li><code>arr.__proto__<\/code> \u53ea\u6709 pop\u3001push \u4e5f\u6ca1\u6709 valueOf\uff0c\u4e8e\u662f\u53bb <code>arr.__proto__.__proto<\/code>__ \u4e0a\u627e;<\/li>\n<li><code>arr.__proto__.__proto<\/code>__ \u5c31\u662f window.Object.prototype;<\/li>\n<li>\u6240\u4ee5 arr.valueOf \u5176\u5b9e\u5c31\u662f window.Object.prototype.valueOf;<\/li>\n<li>arr.valueOf() \u7b49\u4ef7\u4e8e arr.valueOf.call(arr)<\/li>\n<li>arr.valueOf.call(arr) \u7b49\u4ef7\u4e8e window.Object.prototype.valueOf.call(arr)<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/e524432c944ac182d74083347520909.jpg\" alt=\"e524432c944ac182d74083347520909\" \/><\/p>\n<h2>3.JS\u57fa\u7840-\u51fd\u6570\u8fdb\u9636<\/h2>\n<\/li>\n<\/ul>\n<h3>\u51fd\u6570\u5373\u5bf9\u8c61<\/h3>\n<p>\u9996\u5148\uff1aJS\u6240\u6709\u4e8b\u7269\u90fd\u662f\u5bf9\u8c61\uff0c\u5bf9\u8c61\u662f\u62e5\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u6570\u636e\u3002\u6240\u4ee5\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\u3002<\/p>\n<p>\u5f53\u521b\u952e\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u51fd\u6570\u662fFunction\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u6b64\u65f6\u53ef\u4ee5\u628a\u6bcf\u4e00\u4e2a\u521b\u5efa\u51fa\u6765\u7684\u51fd\u6570\uff0c\u5f53\u6210\u662fFunction\u7c7b\u578b\u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u6240\u4ee5\u51fd\u6570\u672c\u8eab\u62e5\u6709\u7684\u5bf9\u8c61\u5c5e\u6027\uff0c\u6765\u6e90\u4e8eFunction\u3002<\/p>\n<p>Fn.Contructor\u51e0\u4f4dFunction<\/p>\n<p>\u4f46\u662f\u4e0e\u6b64\u540c\u65f6\u8981\u6ce8\u610f\uff1a<code>Function.prototype.__proto__===Object.prototype<\/code><\/p>\n<p>\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u6784\u9020\u5668\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u662fObject<\/p>\n<p>\u4e5f\u53ef\u4ee5\u7b80\u5355\u7684\u7406\u89e3\uff1a\u51fd\u6570\u5373\u5bf9\u8c61\u3002<\/p>\n<h3>\u6784\u9020\u51fd\u6570<\/h3>\n<ol>\n<li>\u5728 JavaScript \u4e2d\uff0c\u7528 new \u5173\u952e\u5b57\u6765\u8c03\u7528\u7684\u51fd\u6570\uff0c\u79f0\u4e3a\u6784\u9020\u51fd\u6570\u3002\u6784\u9020\u51fd\u6570\u9996\u5b57\u6bcd\u4e00\u822c\u5927\u5199;<\/li>\n<li>\u6784\u9020\u51fd\u6570\u7684\u6267\u884c\u8fc7\u7a0b:<\/li>\n<\/ol>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230424221346970.png\" alt=\"image-20230424221346970\" \/> <\/p>\n<p>(1)  \u5f53\u4ee5 new \u5173\u952e\u5b57\u8c03\u7528\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u6807\u8bb0\u4e3a Animal \u7684\u5b9e\u4f8b;<\/p>\n<p>(2)  \u51fd\u6570\u4f53\u5185\u90e8\u7684 this \u6307\u5411\u8be5\u5185\u5b58,\u6bcf\u5f53\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5185\u5b58\u7a7a\u95f4;<\/p>\n<p>(3)  \u7ed9 this \u6dfb\u52a0\u5c5e\u6027\uff0c\u5c31\u76f8\u5f53\u4e8e\u7ed9\u5b9e\u4f8b\u6dfb\u52a0\u5c5e\u6027;<\/p>\n<p>(4)  \u7531\u4e8e\u51fd\u6570\u4f53\u5185\u90e8\u7684this\u6307\u5411\u65b0\u521b\u5efa\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u9ed8\u8ba4\u8fd4\u56de this \uff0c\u5c31\u76f8\u5f53\u4e8e\u9ed8\u8ba4\u8fd4\u56de\u4e86\u8be5\u5185\u5b58\u7a7a\u95f4;<\/p>\n<pre><code class=\"language-javascript\">\/\/\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u8981\u5f55\u5165\u4e00\u5e74\u7ea7\u4e00\u73ed\u4e2d\u6bcf\u4e00\u4f4d\u540c\u5b66\u7684\u4e2a\u4eba\u4fe1\u606f\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e9b\u5bf9\u8c61\uff1a\nvar p1 = {name: &#039;zs&#039;, age:6, gender: &#039;\u7537&#039;, hobby: &#039;basketball&#039;};\nvar p2 = {name: &#039;ls&#039;, age:6, gender: &#039;\u5973&#039;, hobby: &#039;dancing&#039;};\nvar p3 = {name: &#039;ww&#039;, age:6, gender: &#039;\u5973&#039;, hobby: &#039;singing&#039;};\nvar p4 = {name: &#039;zl&#039;, age:6, gender: &#039;\u7537&#039;, hobby: &#039;football&#039;};\n\/\/\u6211\u4eec\u89c2\u5bdf\u5230\uff0c\u8fd9\u56db\u4e2a\u5bf9\u8c61\u7ed3\u6784\u7c7b\u4f3c\uff0c\u751a\u81f3\u5982\u53c2\u6570age\u4e5f\u662f\u76f8\u540c\u7684\uff0c\u90a3\u4e48\uff0c\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u51fd\u6570\nfunction Person(name, gender, hobby) {\n    this.name = name;\n    this.gender = gender;\n    this.hobby = hobby;\n    this.age = 6\n}\nvar p1 = new Person(&#039;zs&#039;, &#039;\u7537&#039;, &#039;baketball&#039;)<\/code><\/pre>\n<h3>\u81ea\u6267\u884c\u533f\u540d\u51fd\u6570<\/h3>\n<p>\u58f0\u660e\u5f0f\u4f1a\u5bfc\u81f4\u51fd\u6570\u63d0\u5347\uff0cfunction\u4f1a\u88ab\u89e3\u91ca\u5668\u4f18\u5148\u7f16\u8bd1\u3002\u5373\u6211\u4eec\u7528\u58f0\u660e\u5f0f\u5199\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u533a\u57df\u58f0\u660e\uff0c\u4e0d\u4f1a\u5f71\u54cd\u6211\u4eec\u8c03\u7528<\/p>\n<pre><code class=\"language-javascript\">\/\/\u58f0\u660e\u5f0f\nfunction XXX() { }<\/code><\/pre>\n<p>\u51fd\u6570\u8868\u8fbe\u5f0f\u51fd\u6570\u8868\u8fbe\u5f0f\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\uff0c\u800c\u51fd\u6570\u8868\u8fbe\u5f0f\u4e2d\u7684function\u5219\u4e0d\u4f1a\u51fa\u73b0\u51fd\u6570\u63d0\u5347\u3002\u800c\u662fJS\u89e3\u91ca\u5668\u9010\u884c\u89e3\u91ca\uff0c\u5230\u4e86\u8fd9\u4e00\u53e5\u624d\u4f1a\u89e3\u91ca\u3002\u56e0\u6b64\u5982\u679c\u8c03\u7528\u5728\u51fd\u6570\u8868\u8fbe\u5f0f\u4e4b\u524d\uff0c\u5219\u4f1a\u8c03\u7528\u5931\u8d25\u3002<\/p>\n<pre><code class=\"language-javascript\">var fn1 = function(){}\n\nfn1();\n\nfunction fn1(){}\/\/\u53ef\u4ee5\u6b63\u5e38\u8c03\u7528 \n\nfn2();\n\nvar fn2 = function(){}\/\/\u65e0\u6cd5\u8c03\u7528<\/code><\/pre>\n<p>\u533f\u540d\u51fd\u6570\u7c7b\u578b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/\u666e\u901a\u533f\u540d\u51fd\u6570,\u65e0\u8fd4\u56de\u503c\n>(function () {\n    alert(&#039;\u4f60\u5988\u903c&#039;)\n})()\n&lt;undefined\n\/\/\u611f\u53f9\u53f7\u5f00\u5934\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\n>!function () {\n    alert(&#039;\u4f60\u5988\u903c&#039;)\n}()\n&lt;true\n\/\/~function(){}()\n>~function(){}()\n&lt;-1\n\/\/-function(){}()\n>-function(){}()\n&lt;NaN\n\/\/+function(){}()\n>+function(){}()\n&lt;NaN<\/code><\/pre>\n<h2>4.JS\u57fa\u7840-\u9762\u5411\u5bf9\u8c61<\/h2>\n<h3>\u5c01\u88c5<\/h3>\n<p><strong>\u5c01\u88c5<\/strong>\uff1a\u628a\u5ba2\u89c2\u4e8b\u7269\u5c01\u88c5\u6210\u62bd\u8c61\u7684\u7c7b\uff0c\u9690\u85cf\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4ec5\u5bf9\u5916\u516c\u5f00\u63a5\u53e3\u3002<\/p>\n<p>\u5728ES6\u4e4b\u524d\uff0c\u662f\u4e0d\u5b58\u5728class\u8fd9\u4e2a\u8bed\u6cd5\u7cd6\u7c7b\u7684\u3002\u6240\u4ee5\u5b9e\u73b0\u5927\u591a\u91c7\u7528\u539f\u578b\u5bf9\u8c61\u548c\u6784\u9020\u51fd\u6570<\/p>\n<p><strong>\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong>\uff1a\u53ea\u80fd\u5728\u6784\u9020\u51fd\u6570\u5185\u8bbf\u95ee\u4e0d\u80fd\u88ab\u5916\u90e8\u6240\u8bbf\u95ee(\u5728\u6784\u9020\u51fd\u6570\u5185\u4f7f\u7528var\u58f0\u660e\u7684\u5c5e\u6027)<\/p>\n<p><strong>\u516c\u6709\u5c5e\u6027\u548c\u65b9\u6cd5(\u6216\u5b9e\u4f8b\u65b9\u6cd5)<\/strong>\uff1a\u5bf9\u8c61\u5916\u53ef\u4ee5\u8bbf\u95ee\u5230\u5bf9\u8c61\u5185\u7684\u5c5e\u6027\u548c\u65b9\u6cd5(\u5728\u6784\u9020\u51fd\u6570\u5185\u4f7f\u7528this\u8bbe\u7f6e\uff0c\u6216\u8005\u8bbe\u7f6e\u5728\u6784\u9020\u51fd\u6570\u539f\u578b\u5bf9\u8c61\u4e0a\u6bd4\u5982Cat.prototype.xxx)<\/p>\n<p><strong>\u9759\u6001\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong>\uff1a\u5b9a\u4e49\u5728\u6784\u9020\u51fd\u6570\u4e0a\u7684\u65b9\u6cd5(\u6bd4\u5982Cat.xxx)\uff0c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5c31\u53ef\u4ee5\u8c03\u7528(\u4f8b\u5982Object.assign())<\/p>\n<pre><code class=\"language-javascript\">function Person(name) {\n    this.name = name;\n    this.sex = &#039;man&#039;;\n    var evil = &#039;\u6211\u95ee\u4f60\u5e72\u7239&#039;;\n    var pickNose = function(){\n        console.log(&#039;\u6211\u6211\u6211\u6211&#039;)\n    }\n    return name\n}<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507110950920.png\" alt=\"image-20230507110950920\" \/> <\/p>\n<hr \/>\n<p>\u5728ES6\u4e4b\u540e\uff0c\u5b58\u5728class\u8fd9\u4e2a\u8bed\u6cd5\u7cd6\u7c7b\u3002\u5f53\u4f60\u4f7f\u7528class\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u9ed8\u8ba4\u8c03\u7528constructor\u8fd9\u4e2a\u51fd\u6570\uff0c\u6765\u63a5\u6536\u4e00\u4e9b\u53c2\u6570\uff0c\u5e76\u6784\u9020\u51fa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u5bf9\u8c61(this)\u5e76\u5c06\u5b83\u8fd4\u56de\uff0c\u56e0\u6b64\u5b83\u88ab\u79f0\u4e3aconstructor\u6784\u9020\u65b9\u6cd5(\u51fd\u6570)\u3002<\/p>\n<p>\u4ee5\u4e0b\u4e24\u4e2a\u5bf9\u8c61\u7b49\u4ef7\uff08\u7b2c\u4e8c\u4e2a\u547d\u540d\u6709\u95ee\u9898\u554a\uff1f\uff1f\uff09\uff1a<\/p>\n<pre><code class=\"language-javascript\">class Cat{\n    constructor(){}\n    toString(){}\n    toValue(){}\n}\n\nfunction Cat(){}\nCat.prototype = {\n    constructor(){}\n    toString(){}\n    toValue(){}\n}<\/code><\/pre>\n<h3>\u7ee7\u627f<\/h3>\n<p><strong>\u7ee7\u627f<\/strong>\uff1a\u7ee7\u627f\u5c31\u662f\u5b50\u7c7b\u53ef\u4ee5\u4f7f\u7528\u7236\u7c7b\u7684\u6240\u6709\u529f\u80fd\uff0c\u5e76\u4e14\u5bf9\u8fd9\u4e9b\u529f\u80fd\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<p>\u6bd4\u5982\u6211\u6709\u4e2a\u6784\u9020\u51fd\u6570A\uff0c\u7136\u540e\u53c8\u6709\u4e2a\u6784\u9020\u51fd\u6570B\uff0c\u4f46\u662fB\u60f3\u8981\u4f7f\u7528A\u91cc\u7684\u4e00\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4e00\u79cd\u529e\u6cd5\u5c31\u662f\u8ba9\u6211\u4eec\u81ea\u8eab\u5316\u8eab\u4e3aCV\u4fa0\uff0c\u590d\u5236\u7c98\u8d34\u4e00\u6ce2\u3002\u8fd8\u6709\u4e00\u79cd\u5c31\u662f\u5229\u7528\u7ee7\u627f\uff0c\u6211\u8ba9B\u76f4\u63a5\u7ee7\u627f\u4e86A\u91cc\u7684\u529f\u80fd\uff0c\u8fd9\u6837\u6211\u5c31\u80fd\u7528\u5b83\u4e86\u3002<\/p>\n<ol>\n<li>\u539f\u578b\u94fe\u7ee7\u627f<\/li>\n<li>\u6784\u9020\u7ee7\u627f<\/li>\n<li>\u7ec4\u5408\u7ee7\u627f<\/li>\n<li>\u5bc4\u751f\u7ec4\u5408\u7ee7\u627f<\/li>\n<li>\u539f\u578b\u5f0f\u7ee7\u627f<\/li>\n<li>\u5bc4\u751f\u7ee7\u627f<\/li>\n<li>\u6df7\u5165\u5f0f\u7ee7\u627f<\/li>\n<li>class\u4e2d\u7684extends\u7ee7\u627f<\/li>\n<\/ol>\n<h4><font color='green'>\u539f\u578b\u94fe\u7ee7\u627f\uff1a<\/font><\/h4>\n<pre><code class=\"language-javascript\">function Parent(){\n    this.name = &#039;Parent&#039;;\n    this.sex = &#039;box&#039;;\n}\n\nfunction Child(){\n    this.name = &#039;Child&#039;\n}\n\nChild.prototype = new Parent()\n\nvar child1 = new Child()<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507215444925.png\" alt=\"image-20230507215444925\" \/> <\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507215520537.png\" alt=\"image-20230507215520537\" \/> <\/p>\n<h4><font color='red'><strong>instanceof<\/strong><\/font><\/h4>\n<p><code>instanceof<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u68c0\u6d4b\u6784\u9020\u51fd\u6570\u7684prototype\u5c5e\u6027\u662f\u5426\u51fa\u73b0\u5728\u67d0\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u7684\u539f\u578b\u94fe\u4e0a\u3002<\/p>\n<p>A instanceof B \u5b9e\u4f8b\u5bf9\u8c61A instanceof \u6784\u9020\u51fd\u6570B\u68c0\u6d4bA\u7684\u539f\u578b\u94fe(<strong>proto<\/strong>)\u4e0a\u662f\u5426\u6709B.prototype\uff0c\u6709\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507231705911.png\" alt=\"image-20230507231705911\" \/> <\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507231916595.png\" alt=\"image-20230507231916595\" \/> <\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507231112399.png\" alt=\"image-20230507231112399\" \/> <\/p>\n<h4><font color='green'>class\u4e2d\u7684extends\u7ee7\u627f\uff1a<\/font><\/h4>\n<pre><code class=\"language-javascript\">class PParent{\n    constructor(name){\n        this.name = name\n    }\n    getName(){\n        console.log(this.name)\n    }\n}\nclass CChild extends PParent{\n    constructor(name){\n        super(name)\n        this.sex = &#039;boy&#039;\n    }\n}<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230507232705915.png\" alt=\"image-20230507232705915\" \/><\/p>\n<h3>\u591a\u6001<\/h3>\n<p>class\u4e2d\u7684\u591a\u6001\uff1aextends\u3001super<\/p>\n<p>\u591a\u6001\u7684\u5b9e\u9645\u542b\u4e49\u662f\uff1a\u540c\u4e00\u64cd\u4f5c\u4f5c\u7528\u4e8e\u4e0d\u540c\u7684\u5bf9\u8c61\u4e0a\uff0c\u53ef\u4ee5\u4ea7\u751f\u4e0d\u540c\u7684\u89e3\u91ca\u548c\u4e0d\u540c\u7684\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<p>\u5bf9\u4e8ejs\u591a\u6001\u7684\u8be6\u7ec6\u89e3\u91ca\uff1a<a href=\"https:\/\/juejin.cn\/post\/6844904126011146254\">https:\/\/juejin.cn\/post\/6844904126011146254<\/a><\/p>\n<pre><code class=\"language-javascript\">\/* \ninstanceof\u8fd0\u7b97\u7b26\u7528\u4e8e\u6d4b\u8bd5\u6784\u9020\u51fd\u6570\u7684prototype\u5c5e\u6027\u662f\u5426\u51fa\u73b0\u5728\u5bf9\u8c61\u7684\u539f\u578b\u94fe\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\u3002\n\u7b80\u5355\u7406\u89e3\u4e3a\uff1ainstanceof\u53ef\u4ee5\u68c0\u6d4b\u4e00\u4e2a\u5b9e\u4f8b\u662f\u5426\u5c5e\u4e8e\u67d0\u79cd\u7c7b\u578b\u3002\n\u8fd8\u53ef\u4ee5\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u5b9e\u4f8b\u662f\u5426\u5c5e\u4e8e\u5b83\u7684\u7236\u7c7b\u578b\u3002\n*\/\nfunction makeSound(animal){\n    if (animal instanceof Cat){\n        console.log(&quot;\u55b5\u55b5\u55b5&quot;)}\n    else if (animal instanceof Dog){\n        console.log(&quot;\u6c6a\u6c6a\u6c6a&quot;)}\n}\nclass Cat{}\nclass Dog{}\nmakeSound(new Cat())\n\/\/ &lt; \u55b5\u55b5\u55b5\nmakeSound(new Dog())\n\/\/ &lt; \u6c6a\u6c6a\u6c6a<\/code><\/pre>\n<h2>5. JS\u57fa\u7840-this\u4e0enew<\/h2>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/5c5943225c1e8ec27ac458e8a272df3.jpg\" alt=\"5c5943225c1e8ec27ac458e8a272df3\" \/><\/p>\n<p>this\uff0c\u662f\u6307\u5f53\u524d\u7684\u672c\u8eab\uff0c\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0bthis\u6307\u5411\u7684\u662f\u5168\u5c40\u5bf9\u8c61window\uff0c\u800c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u4f1a\u7ed1\u5b9a\u5230undefined\u3002<\/p>\n<p>this\u76845\u79cd\u7ed1\u5b9a\u65b9\u5f0f\uff1a<\/p>\n<h4>\u9ed8\u8ba4\u7ed1\u5b9a<\/h4>\n<p><strong><em>\u975e\u4e25\u683c\u6a21\u5f0f\u4e0bthis\u6307\u5411\u5168\u5c40\u5bf9\u8c61, \u4e25\u683c\u6a21\u5f0f\u4e0bthis\u4f1a\u7ed1\u5b9a\u5230undefined<\/em><\/strong><\/p>\n<pre><code class=\"language-javascript\">var a = 10;\n\/\/ undefined\nfunction foo() {\n    console.log(this.a)\n}\nfoo\n\/* f  foo() {\n    console.log(this.a)\n} *\/\nfoo()\n\/*\nVM245:2 10\nundefined\n*\/\nwindow.a\n\/\/ 10<\/code><\/pre>\n<p>\u4e25\u683c\u6a21\u5f0f\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">&quot;use strict&quot;\nvar a = 10;\nfunction foo() {\n    console.log(&#039;this1&#039;, &#039;this&#039;)\n    console.log(window.a)\n    console.log(this.a)\n}\nconsole.log(window.foo)\nconsole.log(&#039;this2&#039;, this);\nfoo()<\/code><\/pre>\n<p>\u5f00\u542f\u4e86\u4e25\u683c\u6a21\u5f0f\uff0c\u53ea\u662f\u8bf4\u4f7f\u5f97\u51fd\u6570\u5185\u7684this\u6307\u5411undefined\uff0c\u5b83\u5e76\u4e0d\u4f1a\u6539\u53d8\u5168\u5c40\u4e2dthis\u7684\u6307\u5411\u3002\u56e0\u6b64this1\u4e2d\u6253\u5370\u7684\u662fundefined\uff0c\u800c this2\u8fd8\u662fwindow\u5bf9\u8c61\uff1b\u53e6\u5916\uff0c\u5b83\u4e5f\u4e0d\u4f1a\u963b\u6b62a\u88ab\u7ed1\u5b9a\u5230window\u5bf9\u8c61\u4e0a<\/p>\n<pre><code class=\"language-javascript\">let a = 10;\nconst b = 20;\nfunction foo (){\n    console.log(this.a)\n    console.log(this.b)\n}\nfoo();\nconsole.log(window.a)<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230520214003692.png\" alt=\"image-20230520214003692\" \/> <\/p>\n<pre><code class=\"language-javascript\">var a = 1;\nfunction foo(){\n    var a = 2;\n    console.log(this)\n    console.log(this.a)\n}\nfoo()<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230520215146533.png\" alt=\"image-20230520215146533\" \/> <\/p>\n<pre><code class=\"language-javascript\">var a = 1;\nfunction foo () {\n    var a =2;\n    function inner () {\n        console.log(this.a)\n    }\n    inner()\n}\nfoo()<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230520215728012.png\" alt=\"image-20230520215728012\" \/> <\/p>\n<h4>\u9690\u5f0f\u7ed1\u5b9a<\/h4>\n<p><strong><em>\u5f53\u51fd\u6570\u5f15\u7528\u6709\u4e0a\u4e0b\u6587\u5bf9\u8c61\u65f6, \u5982 obj.foo()\u7684\u8c03\u7528\u65b9\u5f0f, foo\u5185\u7684this\u6307\u5411obj<\/em><\/strong><\/p>\n<pre><code class=\"language-javascript\">function foo () {\n    console.log(&#039;this.a:&#039;, this.a)\n    console.log(&#039;this:&#039;, this)\n}\nvar obj = { a: 1, foo}\nvar a = 2\nobj.foo()\n\/\/ \u76f8\u5f53\u4e8e\uff1a\nvar obj = {\n    a :1,\n    foo: function () {\n        console.log(this.a)\n    }\n}\nvar a = 2\nobj.foo()<\/code><\/pre>\n<p><strong>\u8bed\u6cd5\u7cd6\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">var obj == {foo}\n\u7b49\u540c\u4e8e\nvar obj = { foo: foo }<\/code><\/pre>\n<p><strong><font color='red'>\u9690\u5f0f\u4e22\u5931\uff1a<\/font><\/strong>\u9690\u5f0f\u4e22\u5931\u5176\u5b9e\u5c31\u662f\u88ab\u9690\u5f0f\u7ed1\u5b9a\u7684\u51fd\u6570\u5728\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\u4f1a\u4e22\u5931\u7ed1\u5b9a\u5bf9\u8c61\u3002<\/p>\n<p>\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<p>1.\u4f7f\u7528\u53e6\u4e00\u4e2a\u53d8\u91cf\u6765\u7ed9\u51fd\u6570\u8d77\u522b\u540d\uff0c\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u65f6\u4f1a\u88ab\u9690\u5f0f\u8d4b\u503c\uff0c\u56de\u8c03\u51fd\u6570\u4e22\u5931this\u7ed1\u5b9a\u3002<\/p>\n<pre><code class=\"language-javascript\">function foo () {\n    console.log(this.a)\n};\nvar obj = { a: 1, foo};\nvar a = 2;\nvar foo2 = obj.foo;\n\nobj.foo();\nfoo2();<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230520221713128.png\" alt=\"image-20230520221713128\" \/> <\/p>\n<p>\u539f\u56e0\uff1a\u867d\u7136foo2 \u6307\u5411obj.foo\uff0c\u4f46\u662f\u8c03\u7528\u7684\u65f6\u5019\u662fwindow\u5bf9\u8c61\u5728\u8c03\u7528\uff0c\u6240\u4ee5this\u7684\u6307\u5411\u4e5f\u662fwindow\u5728\u4f7f\u7528\uff0c\u5373this.a\u7b49\u540c\u4e8ewindow.a<\/p>\n<p><strong>\u4e0b\u4e00\u4e2a\u4f8b\u9898\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">function foo () {\n    console.log(this.a)\n};\nvar obj = { a: 1, foo };\nvar a = 2;\nvar foo2 = obj.foo;\nvar obj2 = { a:3, foo2: obj.foo }\nobj.foo();\nfoo2();\nobj2.foo2();<\/code><\/pre>\n<p>\u731c\u4e00\u4e0b\uff1f<\/p>\n<p>obj.foo(); --&gt; 1<br \/>\nfoo2(); --&gt; 2<br \/>\nobj2.foo2(); --&gt; 3<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521003652226.png\" alt=\"image-20230521003652226\" \/> <\/p>\n<p>\u54e6\uff0c\u8499\u5bf9\u4e86\u55f7\uff01\u6d41\u6279\u3002<\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong><\/p>\n<ul>\n<li>obj.foo()\u4e2d\u7684this\u6307\u5411\u8c03\u7528\u8005obj;<\/li>\n<li>foo2()\u53d1\u751f\u4e86\u9690\u5f0f\u4e22\u5931\uff0c\u8c03\u7528\u8005\u662fwindow\uff0c\u4f7f\u5f97foo()\u4e2d\u7684this\u6307\u5411window;<\/li>\n<li>foo3()\u53d1\u751f\u4e86\u9690\u5f0f\u4e22\u5931\uff0c\u8c03\u7528\u8005\u662fobj2,\u4f7f\u5f97foo()\u4e2d\u7684this\u6307\u5411obj2<\/li>\n<\/ul>\n<p><strong>\u4e0b\u4e00\u4e2a\u4f8b\u9898\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">function foo () {\n    console.log(this.a)\n}\nfunction doFoo (fn) {\n    console.log(this)\n    fn()\n}\nvar obj = { a: 1, foo }\nvar a = 2\ndoFoo(obj.foo)<\/code><\/pre>\n<p>\u5f00\u59cb\u731c\uff1a<\/p>\n<p>\u6211\u731c\u662f 2\uff0c\u5c40\u57df\u53d8\u91cf\u7684\u5e94\u8be5\u662f\u4ecedoFoo\u5f00\u59cb\u7684\uff0c\u6b64\u5904\u6ca1\u6709\u4f20\u5165(\u5b9a\u4e49)a\u7684\u503c\uff0c\u6240\u4ee5a\u5e94\u8be5\u662fwindow.a=2,\u540e\u9762\u6253\u5370\u503c\u5e94\u8be5\u4e5f\u662f\u8fd9\u4e2a\u503c<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521004839415.png\" alt=\"image-20230521004839415\" \/> <\/p>\n<p><strong>\u4e0b\u4e00\u4e2a\u4f8b\u9898\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">function foo () {\n    console.log(this.a)\n}\nfunction doFoo (fn) {\n    console.log(this)\n    fn()\n}\nvar obj = { a: 1, foo }\nvar a = 2\nvar obj2 = { a: 3, doFoo }\nobj2.doFoo(obj.foo)<\/code><\/pre>\n<p>\u662f3\u5457\uff1f<\/p>\n<p>\u6211\u9760\u6041\u5a18... ...<\/p>\n<p>\u6211\u4eec\u6253\u5370\u4e2a\u8be6\u7ec6\u7684\u770b\u770b\u7ed3\u679c\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521005559685.png\" alt=\"image-20230521005559685\" \/> <\/p>\n<p>\u6253\u53703\u662fdoFoo\u5e72\u7684\uff0c\u6b64\u65f6\u8fd8\u6253\u5370\u4e86this,\u663e\u793a\u7684\u503c\u5c31\u662f\u4e0a\u9762\u56fe\u4e2d\u7684\u5bf9\u8c61\uff0c\u53ef\u89c1a\u5c31\u662f3\uff1b<\/p>\n<p>\u6253\u53702\u662fobj.foo\u5e72\u7684\uff0c\u6b64\u65f6\u7684this\u5df2\u7ecf\u662fwindow\u5bf9\u8c61\u4e86\uff0c\u6253\u5370a\u5c31\u662f2\uff0c\u4e0d\u884c\u89c1\u56fe\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521005856560.png\" alt=\"image-20230521005856560\" \/> <\/p>\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<p>\u6240\u4ee5\u8bf4\uff0c\u5982\u679c\u4f60\u628a\u4e00\u4e2a\u51fd\u6570\u5f53\u6210\u53c2\u6570\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u53d1\u751f\u9690\u5f0f\u4e22\u5931\u7684\u95ee\u9898\uff0c\u4e14\u4e0e\u5305\u88f9\u7740\u5b83\u7684\u51fd\u6570\u7684this\u6307\u5411\u65e0\u5173\u3002\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u4f1a\u628a\u8be5\u51fd\u6570\u7684this\u7ed1\u5b9a\u5230window\u4e0a\uff0c\u4e25\u683c\u6a21\u5f0f\u4e0b\u7ed1\u5b9a\u5230undefined\u3002<\/p>\n<h4>\u663e\u793a\u7ed1\u5b9a<\/h4>\n<p><strong><em>\u901a\u8fc7call()\u6216\u8005apply()\u65b9\u6cd5\u76f4\u63a5\u6307\u5b9athis\u7684\u7ed1\u5b9a\u5bf9\u8c61, \u5982foo.call(obj)<\/em><\/strong><\/p>\n<p><strong>call, apply, bind<\/strong><\/p>\n<ol>\n<li>\u90fd\u662f\u5bf9\u51fd\u6570\u7684\u64cd\u4f5c\uff0c\u4f7f\u7528\u65b9\u5f0f\uff1a\u51fd\u6570.call<\/li>\n<li>\u90fd\u662f\u7528\u6765\u6539\u53d8\u51fd\u6570\u7684 this \u5bf9\u8c61\u7684\u6307\u5411\u7684\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u662f this \u8981\u6307\u5411\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u90fd\u53ef\u4ee5\u5229\u7528\u540e\u7eed\u53c2\u6570\u4f20\u53c2\u3002<\/li>\n<li>call \u63a5\u53d7\u51fd\u6570\u4f20\u53c2\u65b9\u5f0f\u4e3a\uff1afn.call(this, 1, 2, 3)<\/li>\n<li>apply \u63a5\u53d7\u51fd\u6570\u4f20\u53c2\u65b9\u5f0f\u4e3a\uff1afn.apply(this,[1, 2, 3])<\/li>\n<li>bind \u7684\u8fd4\u56de\u503c\u4e3a\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u9700\u8981\u518d\u6b21\u8c03\u7528\uff1a fn.bind(this)(1, 2, 3)<\/li>\n<\/ol>\n<p>\u4e09\u70b9\u7279\u6027\uff1a<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528<code>.call()<\/code>\u6216\u8005<code>.apply()<\/code>\u7684\u51fd\u6570\u662f\u4f1a\u76f4\u63a5\u6267\u884c\u7684\uff1b<\/p>\n<\/li>\n<li>\n<p><code>bind()<\/code>\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u9700\u8981\u624b\u52a8\u8c03\u7528\u624d\u4f1a\u6267\u884c\uff1b<\/p>\n<\/li>\n<li>\n<p><code>.call()<\/code>\u548c<code>.apply()<\/code>\u7528\u6cd5\u57fa\u672c\u7c7b\u4f3c\uff0c\u4e0d\u8fc7call\u63a5\u53d7\u82e5\u5e72\u4e2a\u53c2\u6570\uff0c\u800capply\u63a5\u53d7\u7684\u662f\u4e00\u4e2a\u6570\u7ec4<\/p>\n<pre><code class=\"language-javascript\">.call(this, 1, 2, 3, 4, 5)\n.apply(this, [1, 2, 3, 4, 5])<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u770b\u4e0b\u83dc\u9e1f\u6559\u7a0b\u548cW3C\u7684\u6559\u7a0b\uff1a<\/p>\n<p><a href=\"https:\/\/www.runoob.com\/js\/js-function-invocation.html\">JavaScript \u51fd\u6570\u8c03\u7528 | \u83dc\u9e1f\u6559\u7a0b (runoob.com)<\/a><\/p>\n<p><a href=\"https:\/\/www.w3school.com.cn\/js\/js_function_call.asp\">JavaScript \u51fd\u6570 Call (w3school.com.cn)<\/a><\/p>\n<p><a href=\"https:\/\/www.w3school.com.cn\/js\/js_function_apply.asp\">JavaScript \u51fd\u6570 Apply (w3school.com.cn)<\/a><\/p>\n<p><a href=\"https:\/\/www.w3school.com.cn\/js\/js_function_bind.asp\">JavaScript Function bind() (w3school.com.cn)<\/a><\/p>\n<pre><code class=\"language-javascript\">function foo () {\n    console.log(this.a)\n}\nvar obj = { a: 1 }\nvar a = 2 \nfoo() \/\/2\nfoo.call(obj) \/\/1\nfoo.apply(obj) \/\/1\nfoo.bind(obj) \/\/\u6b64\u5904\u6253\u5370\u51fd\u6570\u5bf9\u8c61\uff0c()\u8c03\u7528\u65f6\u624d\u4f1a\u8fd0\u884c\u6253\u5370<\/code><\/pre>\n<p>\u5347\u7ea7\uff1a<\/p>\n<pre><code class=\"language-javascript\">var obj1 = {\n    a: 1\n}\nvar obj2 = {\n    a: 2,\n    foo1: function () {\n        console.log(this.a)\n    },\n    foo2: function () {\n        setTimeout(function(){\n            console.log(this)\n            console.log(this.a)            \n        }, 0)\n    }\n}\nvar a = 3\nobj2.foo1()\nobj2.foo2()<\/code><\/pre>\n<p>obj2.foo1()\u6253\u53702\uff1b<\/p>\n<p>obj2.foo2()\uff0c setTimeout\u662fwindow\u7684\u65b9\u6cd5\uff0c\u7b49\u4e8ewindow.setTimeout,\u6240\u4ee5\u6b64\u65f6\u7684this.a\u6307\u7684\u662fwindow.a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521014221082.png\" alt=\"image-20230521014221082\" \/> <\/p>\n<p>\u4e00\u53e5\u8bdd\uff1a<strong>this\u6c38\u8fdc\u6307\u5411\u6700\u540e\u8c03\u7528\u5b83\u7684\u5bf9\u8c61<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u901a\u8fc7call,apply,bind\u65b9\u6cd5\uff0c\u5c06a\u7ed1\u5b9a\u5230\u5bf9\u8c61\u65b9\u6cd5\u4e2d\uff0c\u4e0d\u6253\u5370\u5168\u5c40\u7684a,\u600e\u4e48\u5b8c\u6210\uff1f <\/p>\n<pre><code class=\"language-javascript\">obj2.foo2.call(obj2)\n\/\/\u8fd9\u6837\u884c\u5417\uff1f<\/code><\/pre>\n<p>\u5982\u679c\u662f\u8fd9\u79cd\u5199\u6cd5\u7684\u8bdd\uff0c\u90a3\u4e48\u6539\u53d8\u7684\u5c31\u662ffoo2\u51fd\u6570\u5185\u7684this\u6307\u5411\u4e86\uff0c\u4f46\u662f\u6211\u4eec\u77e5\u9053\uff0cfoo2\u7684\u6307\u5411\u548csetTimeout\u91cc\u51fd\u6570\u7684this\u662f\u6ca1\u6709\u5173\u7cfb\u7684\uff0ci\u90a3\u4f4d\u8c03\u7528\u5b9a\u65f6\u5668\u7684\u59cb\u7ec8\u662fwindow\u3002<\/p>\n<pre><code class=\"language-javascript\">var obj1 = {\n    a: 1\n}\nvar obj2 = {\n    a: 2,\n    foo1: function () {\n        console.log(this.a)\n    },\n    foo2: function () {\n        setTimeout(function(){\n            console.log(this)\n            console.log(this.a)            \n        }.call(obj1), 0)\n    }\n}\nvar a = 3\nobj2.foo1()\nobj2.foo2()<\/code><\/pre>\n<p><strong>\u4e0b\u4e00\u4e2a\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">var obj1 = {\n    a: 1\n}\nvar obj2 = {\n    a: 2,\n    foo1: function () {\n        console.log(this.a)\n    },\n    foo2: function () {\n        function inner () {\n            console.log(this)\n            console.log(this.a)\n        }\n        inner()\n    }\n}\nvar a = 3\nobj2.foo1()\nobj2.foo2()<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521015330342.png\" alt=\"image-20230521015330342\" \/><\/p>\n<p>\u4e0b\u4e00\u4e2a\uff1a<\/p>\n<pre><code class=\"language-javascript\">var obj = {\n    name: &#039;obj&#039;,\n    foo1: () =&gt; {\n        console.log(this.name)\n    },\n    foo2: function () {\n        console.log(this.name)\n        return() =&gt; {\n            console.log(this.name)\n        }\n    }\n}\nvar name = &#039;window&#039;\nobj.foo1()\nobj.foo2()()<\/code><\/pre>\n<h4>new\u7ed1\u5b9a<\/h4>\n<ul>\n<li>\u5982\u679c new \u4e86\u51fd\u6570\u5185\u7684 this \u4f1a\u6307\u5411\u5f53\u524d\u8fd9\u4e2a person \u5e76\u4e14\u5c31\u7b97\u51fd\u6570\u5185\u90e8\u4e0d return \u4e5f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u3002<\/li>\n<li>\u5982\u679c\u4e0d new \u7684\u8bdd\u51fd\u6570\u5185\u7684 this \u6307\u5411\u7684\u662f window\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">function person(firstname,lastname,age,eyecolor)\n{\n    this.firstname=firstname;\n    this.lastname=lastname;\n    this.age=age;\n    this.eyecolor=eyecolor;\n    return [this.firstname,this.lastname,this.age,this.eyecolor,this] \n}\n\nvar myFather=new person(&quot;John&quot;,&quot;Doe&quot;,50,&quot;blue&quot;);\nvar myMother=person(&quot;Sally&quot;,&quot;Rally&quot;,48,&quot;green&quot;);\nconsole.log(myFather) \/\/ this \u8f93\u51fa\u4e00\u4e2a person \u5bf9\u8c61\nconsole.log(myMother) \/\/ this \u8f93\u51fa window \u5bf9\u8c61<\/code><\/pre>\n<h4>\u7bad\u5934\u51fd\u6570\u7ed1\u5b9a<\/h4>\n<p><a href=\"https:\/\/blog.csdn.net\/github_38851471\/article\/details\/79446722\">ES6---\u7bad\u5934\u51fd\u6570()=&gt;{} \u4e0efunction\u7684\u533a\u522b_Run4Freedom\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/a><\/p>\n<p><strong><em>this\u7684\u6307\u5411\u7531\u5916\u5c42\u4f5c\u7528\u57df\u51b3\u5b9a\u7684<\/em><\/strong><\/p>\n<ul>\n<li>\u5b83\u91cc\u9762\u7684this\u662f\u7531\u5916\u5c42\u4f5c\u7528\u57df\u6765\u51b3\u5b9a\u7684\uff0c\u4e14\u6307\u5411\u51fd\u6570\u5b9a\u4e49\u65f6\u7684this\u800c\u975e\u6267\u884c\u65f6\uff1b<\/li>\n<li>\u5b57\u9762\u91cf\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u4f5c\u7528\u57df\u662fwindow\uff0c\u5982\u679c\u91cc\u9762\u6709\u7bad\u5934\u51fd\u6570\u5c5e\u6027\u7684\u8bdd\uff0cthis\u6307\u5411\u7684\u662fwindow\uff1b<\/li>\n<li>\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u4f5c\u7528\u57df\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e14\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u7684this\u662f\u6307\u5411\u65b0\u5efa\u7684\u5bf9\u8c61\u7684\uff0c\u56e0\u6b64this\u6307\u5411\u8fd9\u4e2a\u5bf9\u8c61\uff1b<\/li>\n<li>\u7bad\u5934\u51fd\u6570\u7684this\u662f\u65e0\u6cd5\u901a\u8fc7bind,call,apply\u6765\u76f4\u63a5\u4fee\u6539\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u4f5c\u7528\u57df\u4e2d\u7684this\u7684\u6307\u5411\u6765\u95f4\u63a5\u4fee\u6539<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">var name = &#039;window&#039;\nvar obj = {\n    name: &#039;obj&#039;,\n    foo: function () {\n        console.log(this.name)\n    }\n}\nvar obj2 = {\n    name: &#039;obj2&#039;,\n    foo: () =&gt; {\n        console.log(this.name)\n    }\n}<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230521224845565.png\" alt=\"image-20230521224845565\" \/> <\/p>\n<p><font color='red'><strong>\u6784\u9020\u51fd\u6570\u4e2d\u666e\u901a\u51fd\u6570\u548c\u7bad\u5934\u51fd\u6570\u7684\u533a\u522b<\/strong><\/font><\/p>\n<pre><code class=\"language-javascript\">var name = &#039;window&#039;\nfunction Person(name){\n    this.name = name\n    this.foo1 = function () {\n        console.log(this.name)\n    }\n    this.foo2 = () =&gt; {\n        console.log(this.name)\n    }\n}\nvar person2 = {\n    name: &#039;person2&#039;,\n    foo2: () =&gt; {\n        console.log(this.name)\n    }\n}\nvar person1 = new Person(&#039;person1&#039;)\n\nperson1.foo1()  \/\/ person1\nperson1.foo2()  \/\/ person1\nperson2.foo2()  \/\/ window<\/code><\/pre>\n<p><font color='red'><strong>\u7bad\u5934\u51fd\u6570\u7684this\u65e0\u6cd5\u901a\u8fc7bind\/call\/apply\u6765\u4fee\u6539\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u4f5c\u7528\u57df\u4e2d\u7684this\u7684\u6307\u5411\u6765\u95f4\u63a5\u4fee\u6539<\/strong><\/font><\/p>\n<pre><code class=\"language-javascript\">var name = &#039;window&#039;\nvar obj1 = {\n    name: &#039;obj1&#039;,\n    foo1: function () {\n        console.log(this.name)\n        return() =&gt; {\n            console.log(this.name)\n        }\n    },\n    foo2: () =&gt; {\n        console.log(this.name)\n        return function () {\n            console.log(this.name)\n        }\n    }\n}\nvar obj2 = {\n    name: &#039;obj2&#039;\n}\nobj1.foo1.call(obj2)()\nobj1.foo1().call(obj2)\nobj1.foo2.call(obj2)()\nobj1.foo2().call(obj2)\n\/*\nobj2\nobj2\nobj1\nobj1\nwindow\nwindow\nwindow\nobj2\n*\/<\/code><\/pre>\n<h4>new\u7684\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48<\/h4>\n<ol>\n<li>\u65b0\u751f\u6210\u4e86\u4e00\u4e2a\u5bf9\u8c61<\/li>\n<li>\u94fe\u63a5\u5230\u539f\u578b<\/li>\n<li>\u7ed1\u5b9a this<\/li>\n<li>\u8fd4\u56de\u65b0\u5bf9\u8c61<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">function create(...rest){\n    \/\/\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5bf9\u8c61\n    let obj = new Object()\n    \/\/\u83b7\u5f97\u6784\u9020\u51fd\u6570\n    let Con = rest.shift()\n    \/\/\u94fe\u63a5\u5230\u539f\u578b\n    obj.__proto__ = Con.prototype\n    \/\/\u7ed1\u5b9athis,\u6267\u884c\u6784\u9020\u51fd\u6570\n    let result = Con.apply(obj, arguments)\n    \/\/\u786e\u4fddnew\u51fa\u6765\u662f\u4e2a\u5bf9\u8c61\n    return typeof result === &#039;object&#039; ? result : obj\n}<\/code><\/pre>\n<h2>6. JS\u57fa\u7840-\u7bad\u5934\u51fd\u6570<\/h2>\n<p>ES6\u6807\u51c6\u65b0\u589e\u4e86\u4e00\u79cd\u65b0\u7684\u51fd\u6570\uff1aArrow Function\uff08\u7bad\u5934\u51fd\u6570\uff09\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u53ebArrow Function\uff1f<\/p>\n<p>\u56e0\u4e3a\u5b83\u7684\u5b9a\u4e49\u7528\u7684\u5c31\u662f\u4e00\u4e2a\u7bad\u5934<\/p>\n<pre><code class=\"language-javascript\">x =&gt; x * x<\/code><\/pre>\n<p>\u76f8\u5f53\u4e8e <\/p>\n<pre><code class=\"language-javascript\">function (x) {    \n    return x * x;\n} <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.JS\u57fa\u7840-\u5bf9\u8c61Object \u901a\u8fc7 JavaScript\uff0c\u60a8\u80fd\u591f\u5b9a\u4e49\u5e76\u521b\u5efa\u81ea\u5df1\u7684\u5bf9\u8c61\u3002 \u521b\u5efa\u65b0\u5bf9\u8c61\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff1a \u4f7f\u7528 Objec   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[89,189],"tags":[78,162,161],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8763"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8763"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8763\/revisions"}],"predecessor-version":[{"id":8793,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8763\/revisions\/8793"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8763"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}