请问一下这个 js 代码里面的报错问题 prototype - V2EX
yagamil

请问一下这个 js 代码里面的报错问题 prototype

  •  
  •   yagamil Jul 26, 2023 1983 views
    This topic created in 1022 days ago, the information mentioned may be changed or developed.

    在那边《 Javascript 编程精髓》里面的例子。

    代码: https://output.jsbin.com/zupezayihi

    var print = console.log; function BasicServer() { this.decorator_list = [] this.decorators = {} } BasicServer.prototype.init = function() { for (var i = 0; i < this.decorator_list.length; i++) { var func = this.decorator_list[i] this.decorators[func].init(i) } } BasicServer.prototype.decorated = function(i) { this.decorator_list.push(i) } //BasicServer.prototype.decorators = {} BasicServer.prototype.decorators.nodeServer = { init: function(i) { print('init node Server'); print(i); } } 

    为什么我在函数里面定义的变量
    this.decorators = {}

    在 BasicServer.prototype.decorators.nodeServer 添加元素的时候会提示 nodeServer undefine ?

    用注释掉的方法就可以, BasicServer.prototype.decorators = {}

    对 prototype 这些术语不太熟,可能问的不太明白,见谅了~

    6 replies    2023-07-26 15:52:10 +08:00
    zangbianxuegu
        1
    zangbianxuegu  
       Jul 26, 2023
    BasicServer.prototype.decorators 没有定义
    nikoxie
        2
    nikoxie  
       Jul 26, 2023
    BasicServer.prototype.decorators= {
    nodeServer : {
    init: function(i) {
    print('init node Server');
    print(i);
    }
    }
    }
    echo0x000001
        3
    echo0x000001  
       Jul 26, 2023   1
    prototype 指向类的原型,this 指向类的实例自身,在这个类没有实例化之前,this 是不存在的。
    echo0x000001
        4
    echo0x000001  
       Jul 26, 2023   1
    所以上面的 this.decorators 对于后面的原型定义没有任何影响,甚至实例化后,还会覆盖原型属性,无法直接访问到你定义的 nodeServer
    jixule
        5
    jixule  
       Jul 26, 2023
    直接问 codeium 吧:
    “这是因为在定义 BasicServer.prototype.decorators.nodeServer 之前,您没有初始化 BasicServer.prototype.decorators 对象。因此,在尝试添加元素到未定义的对象时会出现错误。

    通过注释掉的方法,您实际上是先初始化了 BasicServer.prototype.decorators 对象,然后再给它添加了一个属性 nodeServer 。

    如果您想在函数里面定义 this.decorators 并且在后续的代码中添加元素,您可以在函数中先初始化 this.decorators 对象,然后再添加属性。”
    yagamil
        6
    yagamil  
    OP
       Jul 26, 2023 via Android
    @echo0x000001 谢谢大佬。
    谢谢其他回复的老师。
    About     Help     Advertise     Blog     API     FAQ     Solana     3241 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 54ms UTC 13:53 PVG 21:53 LAX 06:53 JFK 09:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86