7

Suppose I create an object factory like so:

var newObj=function(x){
  var obj=[]
  obj.x=x
  obj.add=function(n){
    return this.x+n
  }
  return obj
}

Now suppose I create hundreds of instances of this object:

var obj1=newObj(1)
var obj2=newObj(2)
...

Does each obj1,obj2,... store their own copy of obj.add or do they all contain a reference to a single instance of obj.add stored in memory?

Thanks!

3
  • I am creating a node application with a very large number of very large objects using the factory technique shown above. It occurred to me that I am probably wasting a lot of storage space. It seems I should start filling the prototype property instead. Commented Jul 7, 2013 at 16:09
  • You could have found out easily by comparing obj1.add === obj2.add. Commented Jul 7, 2013 at 18:09
  • See stackoverflow.com/questions/17308446/… Commented Jul 7, 2013 at 19:49

2 Answers 2

2

Both obj and obj2 will instantiate their own copies of obj.add. They are function expressions that get executed at creation and stored in memory for the duration of the object's lifetime.

If you want to maintain performance, what you should use is prototyping:

var newObj=function(x){
  this.obj = [];
  this.obj.x = x;
  return this.obj;
}

newObj.prototype.add = function(n) {
    return this.obj.x += n;
}

This will create a function that all future newObj objects will use without taking up extra space in memory for the exact same function.

Sign up to request clarification or add additional context in comments.

6 Comments

Point of using prototypes is lost if you declare the method both on the prototype and in the function as before, as the latter will override the prototype definition
Thanks for that catch; I've updated my answer to not be so silly.
Thanks. In order for prototyping to work must I use the 'new' keyword technique as in 'new Obj' or does this work with my 'newObj' factory?
A prototype can be bound to any JavaScript object, so as long as you can reference it, you can create it. It's generally advisable to stay away from the new declaration entirely because it can be confusing. What I usually do is something like: function factory(x) { // do stuff } factory.prototype.add = function(n) { // do more stuff } and then just create new objects from what my function returns whenever I need it.
But obj is an array and arrays don't have an add method. Adding add to newObj.prototype does not make the array have that function.
|
1

They will all have their own instances of that method.

If you use prototypes, they will share that method, given you declare it on the prototype object.

So you could do something like

newObj.prototype.add = function(n) {
    return this.x+n
}

And then not declare add on the object as before.

In V8 there is a thing called hidden classes which basically means that all newObj instances will share a hidden class as long as you don't make changes to an instance, which would (as far as I understand) effectively make all the newObj's share the method as with prototyping.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.