开发者社区> 问答> 正文

JS prototype 问题?

function a(){
    this.hello=function(){
        alert("hello!");
    }
}
 
a.prototype={
    fuck:function(){
        alert("oh fuck!");
    },
    hello:function(){
        alert("hello 2!");
    }
}
 
var b=new a();
b.fuck(); //oh fuck!
b.hello();//hello!  这里hello 没有被 a.prototype.hello 覆盖为何?
/*
也就是说 this.hello 并不是 prototype 成员,那么 this.hello 是什么呢?
*/

展开
收起
a123456678 2016-07-15 15:47:00 2009 0
1 条回答
写回答
取消 提交回答
  • function a() {...} 相当于构造函数,在 new 的时候必然会执行一次

    而楼主在构造函数里把原来的 hello 覆盖了,当然不再是 prototype 里面定义那个

    2019-07-17 19:57:07
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载