最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: when to define functions inside constructor and when to use prototype? - Stack Overflow

programmeradmin5浏览0评论

I've noticed different ways to add functions to "classes" in various tutorials. The first is inside the class' constructor:

Class = function () {
    this.doSomething = function() {...};
}

The other one is:

Class = function () {}
Class.prototype.doSomething  = function() {...};

In which situations should one be used over the other?

Is my understanding that there are no protected properties or methods in Javascript correct? If so, what should be used instead?

I've noticed different ways to add functions to "classes" in various tutorials. The first is inside the class' constructor:

Class = function () {
    this.doSomething = function() {...};
}

The other one is:

Class = function () {}
Class.prototype.doSomething  = function() {...};

In which situations should one be used over the other?

Is my understanding that there are no protected properties or methods in Javascript correct? If so, what should be used instead?

Share Improve this question edited Sep 13, 2023 at 20:17 iono 2,7631 gold badge31 silver badges38 bronze badges asked Nov 13, 2012 at 10:47 Eugeny89Eugeny89 3,73110 gold badges55 silver badges104 bronze badges 1
  • 3 About your second question (which really should be in a separate question): Prefix them with an underscore. People will know that those are internal methods that usually shouldn't be accessed. – ThiefMaster Commented Nov 13, 2012 at 10:51
Add a ment  | 

1 Answer 1

Reset to default 14

When you define a function inside the constructor as this.myFunction=..., it is specific to your instance. This means that it must be constructed and kept in memory for all instances, which may be heavy. It also can't be inherited .

The only valid reason to do this are :

  • the enclosing of specific values
  • other types of specific functions (you might build a different function each time)

Most often, what you really need is a function defined on the prototype.

From the MDN on objects :

All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden. For example, other constructors' prototypes override the constructor property and provide their own toString methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.

Regarding your additional question : the following code builds a non directly accessible function :

Class = function () {
   var imprivate = function(){...};
   this.doSomething = function() { uses imprivate};
}

A downside is that you have a different function instance for each instance of Class. This is most often done for modules (from which you have only one instance). Personally, I prefer to do exactly as suggested by ThiefMaster in ment : I prefix my private functions with _ :

// private method
XBasedGrapher.prototype._ensureInit = function() {
发布评论

评论列表(0)

  1. 暂无评论