在构造函数内外为 prototype 属性赋值的区别 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a Javascript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
Javascript 权威指南第 5 版
Closure: The Definitive Guide
coolzjy
V2EX    Javascript

在构造函数内外为 prototype 属性赋值的区别

  •  
  •   coolzjy 2015 年 4 月 18 日 3157 次点击
    这是一个创建于 3954 天前的主题,其中的信息可能已经有所发展或是发生改变。

    function Person(){ Person.prototype.talk=function(){alert("saying")}; } function Men(){ } Men.prototype = new Person(); var men = new Men(); men.talk(); 

    可以执行成功,但

    function Person(){ Person.prototype.talk=function(){alert("saying")}; } function Men(){ Men.prototype = new Person(); } var men = new Men(); men.talk(); 

    报错。请问在构造函数内部和外部为 prototype 赋值有何区别

    4 条回复    2015-04-19 11:36:20 +08:00
    iyangyuan
        1
    iyangyuan  
       2015 年 4 月 18 日 via iPhone   1
    因为第二个在new的时候,你还没有指定原型。

    new相当于创建一个新对象,然后把这个新对象的原型指向函数(类)的prototype对象,第二个在构造方法里才指定原型,此时新对象的创建已经完成,它早已指向别处,所以报错。

    在第二个例子中,如果再new一次,应该就不会报错了。
    bombless
        2
    bombless  
       2015 年 4 月 18 日
    function Men(){
    function ret() {}
    ret.prototype = new Person();
    ret = new ret;
    Men = ret;
    return ret
    }
    作弊可以吗,哈哈哈
    xavierchow
        3
    xavierchow  
       2015 年 4 月 18 日
    请看https://developer.mozilla.org/en-US/docs/Web/Javascript/Reference/Operators/new
    ```
    When the code new foo(...) is executed, the following things happen:

    1. A new object is created, inheriting from foo.prototype.
    2.The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo(), i.e. if no argument list is specified, foo is called without arguments.
    3.The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
    ```
    第一步的时候,Men的prototype还不是Person对象,第二步才是执行constructor,而在你的代码中这时才改Men的prototype, 晚了。
    xavierchow
        4
    xavierchow  
       2015 年 4 月 19 日
    补充一点,对象初始化之后prototype是不可以改变,但是可以修改prototype的属性。
    所以new之后再assign prototype是不行的,但是(new Person()).talk()是OK的。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2369 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 63ms UTC 08:04 PVG 16:04 LAX 00:04 JFK 03:04
    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