Js_==&===

via: http://ilovejsj.iteye.com/blog/473474

 

首先,== equality 等同,=== identity 恒等。 

==, 两边值类型不同的时候,要先进行类型转换,再比较。 

==,不做类型转换,类型不同的一定不等。 

 

下面分别说明: 

先说 ===,这个比较简单。下面的规则用来判断两个值是否===相等: 

1、如果类型不同,就[不相等] 

2、如果两个都是数值,并且是同一个值,那么[相等];(!例外)的是,如果其中至少一个是NaN,那么[不相等]。(判断一个值是否是NaN,只能用isNaN()来判断) 

3、如果两个都是字符串,每个位置的字符都一样,那么[相等];否则[不相等]。 

4、如果两个值都是true,或者都是false,那么[相等]。 

5、如果两个值都引用同一个对象或函数,那么[相等];否则[不相等]。 

6、如果两个值都是null,或者都是undefined,那么[相等]。 

再说 ==,根据以下规则: 

1、如果两个值类型相同,进行 === 比较。 

2、如果两个值类型不同,他们可能相等。根据下面规则进行类型转换再比较: 

   a、如果一个是null、一个是undefined,那么[相等]。 

   b、如果一个是字符串,一个是数值,把字符串转换成数值再进行比较。 

   c、如果任一值是 true,把它转换成 1 再比较;如果任一值是 false,把它转换成 0 再比较。 

   d、如果一个是对象,另一个是数值或字符串,把对象转换成基础类型的值再比较。对象转换成基础类型,利用它的toString或者valueOf方法。js核心内置类,会尝试valueOf先于toString;例外的是Date,Date利用的是toString转换。非js核心的对象,令说(比较麻烦,我也不大懂) 

   e、任何其他组合,都[不相等]。 

 

举例: 

"1" == true 

  类型不等,true会先转换成数值 1,现在变成 "1" == 1,再把"1"转换成 1,比较 1 == 1, 相等。 

 

= 赋值运算符 

== 等于 

=== 严格等于 

例: 

var a = 3; 

var b = "3"; 

 

a==b 返回 true 

a===b 返回 false 

 

因为a,b的类型不一样 

===用来进行严格的比较判断

 

--------------------------------------------------------------

1、对于string,number等基础类型,==和===是有区别的

1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等

2)同类型比较,直接进行“值”比较,两者结果一样

 

2、对于Array,Object等高级类型,==和===是没有区别的

进行“指针地址”比较

 

3、基础类型与高级类型,==和===是有区别的

1)对于==,将高级转化为基础类型,进行“值”比较

2)因为类型不同,===结果为false

index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: Cannot read properties of undefined (reading 'prototype') at runInContext2 (vendor.js:22936:32) at Object.<anonymous> (vendor.js:27708:14) at vendor.js:27715:6 at 69049 (vendor.js:27716:2) at __webpack_require__ (bootstrap:19:1) at 35982 (app.js:3:23) at __webpack_require__ (bootstrap:19:1) at Object.appCallback [as app] (app.json?f9f8:26:11) at se (af-appx.worker.min.js:6:1676713) at af-appx.worker.min.js:6:1678991 $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor._export_sfc is not a function at 49388 (navigation-bar.js:134:49) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:306:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) at 31940 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:304:17) at __webpack_require__ (bootstrap:19:1) at 27038 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:518:33) at __webpack_require__ (bootstrap:19:1) at pages/index/index (app.json?f9f8:40:17) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor._export_sfc is not a function at 68408 (u-icon.js:244:49) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1307:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) at 58465 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1305:17) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1413:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor._export_sfc is not a function at 82080 (z-paging-swiper.js:93:49) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:351:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) at 80783 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:349:17) at __webpack_require__ (bootstrap:19:1) at 72530 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:747:33) at __webpack_require__ (bootstrap:19:1) at pages/kb/kb (app.json?f9f8:46:17) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor._export_sfc is not a function at 70099 (empty.js:7:55) at __webpack_require__ (bootstrap:19:1) at 77238 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:442:33) at __webpack_require__ (bootstrap:19:1) at pages/empty/empty (app.json?f9f8:49:17) at e.ensurePageHostCallback (af-appx.worker.min.js:6:430759) at af-appx.worker.min.js:6:413475 at Object.value (af-appx.worker.min.js:6:411837) at e.getPageLogicOption (af-appx.worker.min.js:6:419292) at zN (af-appx.worker.min.js:6:1681529) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor._export_sfc is not a function at 96759 (u-avatar.js:181:49) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1157:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) at 19646 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1155:17) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:290:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor.defineComponent is not a function at 73019 (webview.js:3:49) at __webpack_require__ (bootstrap:19:1) at 24127 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:966:33) at __webpack_require__ (bootstrap:19:1) at pages/webview/webview (app.json?f9f8:55:17) at e.ensurePageHostCallback (af-appx.worker.min.js:6:430759) at af-appx.worker.min.js:6:413475 at Object.value (af-appx.worker.min.js:6:411837) at e.getPageLogicOption (af-appx.worker.min.js:6:419292) at zN (af-appx.worker.min.js:6:1681529) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: common_vendor._export_sfc is not a function at 19691 (u-divider.js:93:49) at __webpack_require__ (bootstrap:19:1) at index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1262:3 at e.appendLazyComponentImpl (af-appx.worker.min.js:6:417127) at o.appendLazyComponent (af-appx.worker.min.js:6:415422) at 10868 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1260:17) at __webpack_require__ (bootstrap:19:1) at 71652 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:897:33) at __webpack_require__ (bootstrap:19:1) at pages/login/login (app.json?f9f8:58:17) $AppxFramework.bootstrapInAppContextByProtocol @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 bootstrap @ app.json?6035:107 __webpack_modules__.13359.module.exports @ app.json?6035:131 (anonymous) @ app.json?f9f8:68 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ app.json?f9f8:76 (anonymous) @ index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:13 index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:2 TypeError: Cannot read properties of undefined (reading 'getSystemInfoSync') at getDevice (weCropper.js:23:34) at 89846 (weCropper.js:28:11) at __webpack_require__ (bootstrap:19:1) at 82986 (u-avatar-cropper.js:3:67) at __webpack_require__ (bootstrap:19:1) at 7173 (index.worker.js?hash=15c40696&from_service_worker=true&url=file%3A%2F%2F%2Fhttp%3A%2F%2F127.0.0.1%3A61955%2Findex.html&page=pages%2Findex%2Findex&ap_framework_sceneId=0000&enablePolyfillWorker=true&ap_framework_sceneId=0000:1142:33) at __webpack_require__ (bootstrap:19:1) at uni_modules/vk-uview-ui/components/u-avatar-cropper/u-avatar-cropper (app.json?f9f8:61:17) at e.ensurePageHostCallback (af-appx.worker.min.js:6:430759) at af-appx.worker.min.js:6:413475
最新发布
11-18
在 JavaScript 中,类的继承机制是基于原型(prototype)和原型链(prototype chain)实现的。虽然 ES6 引入了 `class` 关键字,使语法更接近传统面向对象语言,但其底层仍然依赖于原型系统。 ### 类与原型的关系 JavaScript 中的类本质上是一个函数,它具有 `prototype` 属性。当使用 `class` 定义一个类时,JavaScript 会自动创建一个与该类关联的原型对象,并将其赋值给类的 `prototype` 属性。这个原型对象默认包含一个 `constructor` 属性,指向类本身[^1]。 ```javascript class Person { constructor(name) { this.name = name; } } console.log(Person.prototype); // { constructor: Person, ... } ``` 每个类的实例都会有一个内部属性 `__proto__`,它指向类的原型对象。这样实例就可以访问原型上的属性和方法[^1]。 ```javascript const p = new Person('Alice'); console.log(p.__proto__ === Person.prototype); // true ``` ### 原型链的指向关系 JavaScript 的原型链是由多个对象通过 `__proto__` 指针连接起来形成的结构。当访问一个对象的属性或方法时,如果该对象自身没有这个属性或方法,则 JavaScript 引擎会沿着 `__proto__` 链向上查找,直到找到为止,或者到达原型链的末端(即 `null`)。 #### Function 与 Object 的关系 - 所有函数(包括构造函数、类)都是 `Function` 的实例,因此它们的 `__proto__` 指向 `Function.prototype`。 - `Function.prototype` 是一个特殊的函数对象,它的 `__proto__` 指向 `Object.prototype`。 - `Object.prototype.__proto__` 是 `null`,表示原型链的终点。 ```javascript console.log(Person.__proto__ === Function.prototype); // true console.log(Function.prototype.__proto__ === Object.prototype); // true console.log(Object.prototype.__proto__); // null ``` #### 类之间的继承关系 当使用 `class` 实现继承时,子类会通过原型链连接到父类的原型对象。ES6 提供了 `extends` 和 `super` 来简化继承过程。 ```javascript class Animal { speak() { console.log('Make a sound'); } } class Dog extends Animal { speak() { console.log('Bark'); } } const d = new Dog(); d.speak(); // Bark console.log(Dog.__proto__ === Animal); // true console.log(Dog.prototype.__proto__ === Animal.prototype); // true ``` 在这个例子中: - `Dog.__proto__` 指向 `Animal`,表示 `Dog` 是 `Animal` 的子类。 - `Dog.prototype.__proto__` 指向 `Animal.prototype`,表示 `Dog` 实例的原型链连接到了 `Animal` 的原型。 #### Function 与 Object 的特殊性 由于 `Function` 和 `Object` 是 JavaScript 内置的核心构造函数,它们之间存在循环引用的情况: - `Function.__proto__ === Function.prototype` - `Object.__proto__ === Function.prototype` - `Function.prototype.__proto__ === Object.prototype` - `Object.prototype.__proto__ === null` 这种设计反映了 JavaScript 原型系统的复杂性和历史遗留问题,但它也确保了所有函数和对象能够正确地参与到原型链中[^2]。 ### 总结 JavaScript 的类和原型继承机制是基于原型链构建的。类本质上是函数,具有 `prototype` 属性;而实例则通过 `__proto__` 指针连接到类的原型对象。通过这种方式,JavaScript 实现了类似其他面向对象语言中的继承特性。同时,`Function` 和 `Object` 的特殊关系体现了 JavaScript 原型系统的灵活性和复杂性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值