【问题标题】:Where can I place prototype property as per my below code? [duplicate]根据下面的代码,我可以在哪里放置原型属性? [复制]
【发布时间】:2018-01-28 16:06:21
【问题描述】:

我用两种不同的方式编写了这两种方法,都给了我相同的结果,所以我什么时候可以使用?

第一个例子

var BaseCls = function() {
  BaseCls.prototype.name = "John";
};
var JustCls = new BaseCls();
console.log(JustCls.name); // This is giving result John

第二个例子

var BaseCls = function() {};
BaseCls.prototype.name = "John";
var JustCls = new BaseCls();
console.log(JustCls.name); // This is also giving result John

两者都给了我相同的结果,所以我只想知道是否有任何其他标准导致在主函数内部/外部使用原型编写此属性/方法?

感谢考虑

【问题讨论】:

  • 第一个“模式”从未被任何人使用

标签: javascript function oop properties prototype


【解决方案1】:

您应该只在构造函数之外更改原型。 否则每次创建实例时都要更改它。

【讨论】:

    猜你喜欢
    • 2017-03-12
    • 2018-08-12
    • 1970-01-01
    • 2018-01-07
    • 2011-09-15
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多