{"id":2209,"date":"2017-05-03T13:18:31","date_gmt":"2017-05-03T05:18:31","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=2209"},"modified":"2017-05-03T13:18:31","modified_gmt":"2017-05-03T05:18:31","slug":"new%e7%9a%84%e8%bf%87%e7%a8%8b%ef%bc%8c%e4%bb%a5%e5%8f%8a__proto__%e4%b8%8eprototype%e7%9a%84%e5%85%b3%e7%b3%bb","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=2209","title":{"rendered":"new\u7684\u8fc7\u7a0b\uff0c\u4ee5\u53ca__proto__\u4e0eprototype\u7684\u5173\u7cfb"},"content":{"rendered":"<p>\u8bf4\u8d77new\uff0c\u8981\u5148\u8bf4\u539f\u578b\u94fe\u3002<br \/>\n\u673a\u5236\u5c31\u662f\u6307\u5bf9\u8c61\u7684\u4e00\u4e2a\u5185\u90e8\u94fe\u63a5\u5f15\u7528\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u3002<br \/>\n\u5982\u679c\u5728\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u6ca1\u6709\u627e\u5230\u9700\u8981\u7684\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u7684\u5f15\u7528\uff0c\u5f15\u64ce\u5c31\u4f1a\u7ee7\u7eed\u5728[[prototype]]\u5173\u8054\u7684\u5bf9\u8c61\u4e0a\u7ee7\u7eed\u67e5\u627e\uff0c\u540c\u7406\uff0c\u5982\u679c\u540e\u8005\u4e2d\u4e5f\u6ca1\u6709\u627e\u5230\u9700\u8981\u7684\u5f15\u7528\u5c31\u4f1a\u7ee7\u7eed\u67e5\u627e\u4ed6\u7684[[prototype]].\u4ee5\u6b64\u7c7b\u63a8\uff0c\u8fd9\u4e00\u7cfb\u5217\u5bf9\u8c61\u7684\u94fe\u63a5\u88ab\u79f0\u4e3a\u201c\u539f\u578b\u94fe\u201d\u3002<br \/>\n\u6765\u770b\u770b\u6211\u4eec\u518d\u719f\u6089\u4e0d\u8fc7\u7684\u4ee3\u7801\uff0c\u901a\u8fc7new\u8fd0\u7b97\u7b26\u4ece\u6784\u9020\u5668\u4e2d\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<pre>function Person(name) {\r\n    this.name = name;\r\n}\r\nPerson.prototype.getName = function() {\r\n    return this.name;\r\n}\r\nvar a = new Person(\"liwen\");\r\nconsole.log(a.name); \/\/liwen<\/pre>\n<p>\u4e0b\u9762\uff0c\u901a\u8fc7\u8fd9\u6bb5\u4ee3\u7801\u6765\u7406\u89e3new\u8fd0\u7b97\u7684\u8fc7\u7a0b<\/p>\n<pre>function Person(name) {\r\n   this.name = name;\r\n}\r\nPerson.prototype.getName = function() {\r\n   return this.name;\r\n}\r\nvar objectFactory = function() {\r\n   \/\/\u4eceObject.prototype\u4e0a\u514b\u9686\u4e00\u4e2a\u7a7a\u7684\u5bf9\u8c61,\u56e0\u4e3aJavaScript\u4e2d\u6240\u6709\u5bf9\u8c61\u7684\u6839\u5bf9\u8c61\u662fObject.prototype\u3002\r\n   var obj = new Object();\r\n   \/\/\u53d6\u5f97\u5916\u90e8\u4f20\u5165\u7684\u6784\u9020\u5668\uff0c\u6b64\u4f8b\u5b50\u662fperson\r\n   var Constructor = [].shift.call(arguments);        \r\n   obj.__proto__ = Constructor.prototype;\/\/\u6307\u5411\u6b63\u786e\u7684\u539f\u578b\uff08\u5373Person.prototype\uff09\uff0c\u9ed8\u8ba4\u662fObject.prototype\r\n   \/\/\u501f\u7528\u5916\u90e8\u4f20\u5165\u7684\u6784\u9020\u5668\u7ed9OBJ\u8bbe\u7f6e\u5c5e\u6027\r\n   var ret = Constructor.apply(obj, arguments);\r\n   \/\/\u786e\u4fdd\u6784\u9020\u5668\u603b\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\r\n   return typeof ret === 'object' ? ret : obj;\r\n}\r\nvar a = objectFactory(Person, \"liwen\");  \/\/\u7b49\u4e8e var a = new Person(\"liwen\")\r\nconsole.log(a.name)\/\/liwen<\/pre>\n<p>\u4e0a\u9762\u5b9e\u73b0\u7684objectFactory\u51fd\u6570\u548cnew \u4ea7\u751f\u4e00\u6837\u7684\u7ed3\u679c\u3002<br \/>\n\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86<b>obj.__proto__ = Constructor.prototype;<\/b>\u6765\u6539\u53d8obj\u539f\u578b\u7684\u6307\u5411\u3002<\/p>\n<p>\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u4f1a\u5728\u5176\u5185\u90e8\u521d\u59cb\u5316\u4e00\u4e2a\u5c5e\u6027__proto__\u3002\u53c8\u56de\u5230\u4e86\u5f00\u5934\u7684\u90a3\u53e5\u8bdd\uff0c\u5f53\u6211\u4eec\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5c31\u901a\u8fc7\u8fd9\u4e2a\u5185\u90e8\u94fe\u63a5__proto__\u5f15\u7528\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u7ee7\u7eed\u627e\u3002<br \/>\n\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5f15\u64ce\u5c31\u4f1a\u7ee7\u7eed\u5728__proto__\u5173\u8054\u7684\u5bf9\u8c61\u4e0a\u901a\u8fc7\u4ed6\u7684__proto__\u7ee7\u7eed\u67e5\u627e\uff0c\u4e00\u76f4\u5230\u6839\u5bf9\u8c61object\u4e3a\u6b62\uff0c\u8fd9\u4e00\u7cfb\u5217\u5bf9\u8c61\u7684\u94fe\u63a5\u88ab\u79f0\u4e3a\u201c\u539f\u578b\u94fe\u201d\u3002<br \/>\n\u800c<b>__proto__\u5c31\u662f\u7528\u6765\u8bb0\u5f55\u94fe\u63a5\u7684\u5185\u90e8\u79c1\u6709\u5c5e\u6027<\/b>\u3002\u8fd9\u662f\u539f\u578b\u94fe\u7684\u672c\u8d28\uff0c<b>\u800cprototype\u662f\u4e00\u4e2a\u5f00\u53d1\u63a5\u53e3<\/b>\u3002<\/p>\n<p>\u800cnew\u7684\u6b65\u9aa4\uff0c\u4e5f\u662f\u521b\u5efa\u4e00\u4e2aobj\uff0c\u5e76\u628aobj\u7684__proto__\u6307\u5411\u8981\u7ee7\u627f\u5bf9\u8c61\u7684\u539f\u578b\uff08Constructor.prototype\u5373Person.prototype\uff09\u3002\u5e76\u8fd4\u56deobj\uff0c\u5b9e\u73b0\u4e86new\u3002<\/p>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=2209\">new\u7684\u8fc7\u7a0b\uff0c\u4ee5\u53ca__proto__\u4e0eprototype\u7684\u5173\u7cfb<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u8d77new\uff0c\u8981\u5148\u8bf4\u539f\u578b\u94fe\u3002 \u673a\u5236\u5c31\u662f\u6307\u5bf9\u8c61\u7684\u4e00\u4e2a\u5185\u90e8\u94fe\u63a5\u5f15\u7528\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u3002 \u5982\u679c\u5728\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u6ca1\u6709\u627e\u5230\u9700\u8981\u7684\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u7684\u5f15\u7528\uff0c\u5f15\u64ce\u5c31\u4f1a\u7ee7\u7eed\u5728[[prototype]]\u5173\u8054\u7684\u5bf9\u8c61\u4e0a\u7ee7\u7eed\u67e5\u627e\uff0c\u540c\u7406\uff0c\u5982\u679c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,284,285,24],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2209"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2209"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2209\/revisions"}],"predecessor-version":[{"id":2210,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2209\/revisions\/2210"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2209"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}