V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
rookiemaster
V2EX  ›  JavaScript

有人能帮忙解释一下这段 js 的打印结果吗,问了几次 gpt 都是错误的,

  •  
  •   rookiemaster · 13 天前 · 1790 次点击
    class A {
    }
    
    class B extends A{
    }
    
    let b = new B()
    console.log( b.__proto__)
    

    打印结果是A{}, 不是很能理解它的含义是什么, b.__proto___不是等于 B.prototype 吗

    11 条回复    2024-09-04 10:36:00 +08:00
    USDT
        1
    USDT  
       13 天前
    对呀。。B.prototype 就是 A 啊,你 console.log 一下 B.prototype 就知道了
    zephyru
        3
    zephyru  
       13 天前
    prototype 不就是像上找一层么?还有什么别的解释?你 new 个 A 试试,在拿 prototype 估计就是空对象了 __proto___ 这个也是
    newaccount
        4
    newaccount  
       13 天前
    你看看 firefox 下是不是你认为的结果
    rabbbit
        5
    rabbbit  
       13 天前
    B.prototype.__proto__ === A.prototype
    daolanfler
        6
    daolanfler  
       13 天前
    B.prototype instanceof A // true
    B.prototype.__proto__ === A.prototype // true
    A.prototype instanceof Object // true

    const a = new A()
    chrome dev console 里面也是 a 也是 A{} 这种形式展示的
    rookiemaster
        7
    rookiemaster  
    OP
       13 天前
    谢谢各位,清晰了
    lyxxxh2
        8
    lyxxxh2  
       13 天前   ❤️ 1
    刚解决了个 bug 。
    基类:
    def init():
    self.config = Config().config
    self.config.update(self.get_config())

    多个类继承基,a 类总是被 b 类改变。
    debug 半天找不到问题。
    又怀疑基类变量的共享,但变量也不在基类啊。
    最后想起... Config()特码的是单例啊。
    mizuki9
        9
    mizuki9  
       13 天前 via Android
    运行了一下,打印出来不是 B{}吗?
    DOLLOR
        10
    DOLLOR  
       13 天前
    对的没错,b.__proto___ 和 B.prototype 都是 A 的实例。

    Object.getPrototypeOf(b) === B.prototype
    > true

    B.prototype instanceof A
    > true
    guiyumin
        11
    guiyumin  
       12 天前
    恕我直言,这有意义吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   999 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:59 · PVG 02:59 · LAX 11:59 · JFK 14:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.