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

js 原型 constructor 的困惑

  •  
  •   lbfeng 2019-06-24 23:17:27 +08:00 3520 次点击
    这是一个创建于 2377 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function Foo(name) { this.name = name; } Foo.prototype.myName = function() { return this.name; }; function Bar(name,label) { Foo.call( this, name ); this.label = label; } console.log(Bar.prototype.constructor); Bar.prototype = Object.create( Foo.prototype ); console.log(Bar.prototype.constructor); Bar.prototype.myLabel = function() { return this.label; }; var a = new Bar( "a", "obj a" ); a.myName(); // "a" a.myLabel(); // "obj a" console.log(a.constructor);" 

    第 1 次输出 Bar.prototype 的 constructor 指向 Bar 完全可以理解。 function Bar(name,label){Foo.call(this,name);this.label=label;}

    第 2, 3 次输出 Bar.prototype 的 constructor 指向 Foo 也完全可以理解。 function Foo(name){this.name=name;} function Foo(name){this.name=name;}

    如果 new Bar 有用到 Bar.prototype.constructor 的话 label 是怎么放入 a 的? 要是没用到的话 constructor 有什么意义?

    画了个图帮助理解 i.imgur。com/k5LMrEV.jpg

    6 条回复    2019-07-19 05:42:29 +08:00
    lbfeng
        1
    lbfeng  
    OP
       2019-06-24 23:24:56 +08:00
    imgur。com/a/dQZ821s
    connection
        2
    connection  
       2019-06-25 00:24:59 +08:00
    Bar.prototype = Object.create( Foo.prototype );

    访问 a 实例的时候访问 constructor 实际上在访问 Foo.prototype 上的 constructor 了吧
    lbfeng
        3
    lbfeng  
    OP
       2019-06-25 04:42:41 +08:00
    @connection 是的,第二个输出证明这点。
    azh7138m
        4
    azh7138m  
       2019-07-09 15:46:35 +08:00
    > label 是怎么放入 a

    不是你自己写的吗?
    function Bar(name,label) {
    Foo.call( this, name );
    this.label = label;
    }

    this.label = label;
    lbfeng
        5
    lbfeng  
    OP
       2019-07-10 00:32:24 +08:00
    @azh7138m 第 4 次输出没写全。a.constructor 是 Foo 而不是 Bar. 这才是我要问的问题。是 Bar 的话我就不用问了。。。
    lbfeng
        6
    lbfeng  
    OP
       2019-07-19 05:42:29 +08:00
    我自己终结吧。var a = new Bar(…)。这个和 new 的过程有关。

    1. Create a new empty object.
    2. Assign the “ this ” value of the constructor function to the new empty object (so this points to the new object).
    3. Execute the code inside the constructor function (adds properties to the new object).
    4. Return the new object if constructor function doesn ’ t have a explicit return statement.

    所以整个创建过程和 prototype.constructor 没啥关系。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     918 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 22:39 PVG 06:39 LAX 14:39 JFK 17:39
    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