0

I have several instances of an object:

function objectname=(prop1,prop2){
    this.prob1=prob1
    this.prob2=prob2
}

var object1=objectname(
    "11",
    "12");
var object2=objectname(
    "21",
    "22");

Now i want to output it. This works:

document.write(object1.prob1); //output 11

Now i want a loop over all different instances like

for (var key in objectname){
    document.write(objectname.key.prob1)}  //Does not work

The problem is that an easy example also does not work:

document.write(objectname.object1.prob1); //Does not work

Why?

user2867054
  • 59
  • 1
  • 7
  • The second one doesn't work because `objectname` doesn't have a property `object1`. `objectname` does not magically have a list of instances. You have to create on if you want one. – Felix Kling Jul 17 '14 at 07:49

3 Answers3

2

There are certain mistakes in your code and you need to fix them first to achieve what you want. First you have a mistake in defining your function, there is an extra = sign there, these are all the possible ways to define your function:

function ObjectName (prop1,prop2){
    this.prob1=prob1
    this.prob2=prob2
}

var ObjectName = function (prop1,prop2){
    this.prob1=prob1
    this.prob2=prob2
};

var ObjectName = function ObjectName (prop1,prop2){
    this.prob1=prob1
    this.prob2=prob2
};

The other point is related to this keyword. Whenever you use this keyword in your function you are able to use your functions in couple of certain ways:

1- Using new keyword: It seems based on what you mean by instances, your code is missing a new keyword:

function ObjectName(prop1,prop2){
    this.prob1=prob1
    this.prob2=prob2
}

var object1=new ObjectName(
    "11",
    "12");
var object2=new ObjectName(
    "21",
    "22");

2- Using call or apply:

var object1 = {}
ObjectName.call(object1,
    "11",
    "12");

or:

var object2 = {}
ObjectName.apply(object2,[
    "21",
    "22"
]);

Or if you don't want to use new keyword, to have a safe object creation process you better use Object.create(prototypeObject) like:

var object1 = Object.create(ObjectName.prototype);
ObjectName.call(object1,
    "11",
    "12");

This would allow you to define your other methods and variables in ObjectName.prototype like:

ObjectName.prototype.myVar = "Anything";

At the end To loop over all the instance you should first store them somewhere. You could do it like:

function ObjectName(prop1,prop2){
    ObjectName.instances.push(this);
    this.prob1=prob1
    this.prob2=prob2
}
ObjectName.instances = [];

Then you can loop over ObjectName.instances which is an array consisting all of your instances.

var instance,
    i = 0, l = ObjectName.instances.length;

for ( ; i < l ; i++ ){
    instance = ObjectName.instances[i];
    //document.write(objectname.key.prob1)}
    console.log(instance.prob1);
}

NEVER use document.write for your outputs, it has different effects in different situation on your page, depending on when you call it. Create you DOM objects like div elements and put your data inside them.

The other point that is worth nothing is the way I have defined your function in camel-case, with a upper first letter. This is kind of convention for creating JavaScript functions that are going to be used as a constructor.

Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • Thanks so far. I did forget to write down new. The loop still does not work. for (var key in ObjectName.instances){ document.write(ObjectName.instances.key[prop1]);} – user2867054 Jul 17 '14 at 08:13
  • @user2867054: check out the loop and think of doing something safe to output your data instead of `document.write`. – Mehran Hatami Jul 17 '14 at 08:24
0

This should work:

objectname.key[prob1]
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

I suspect that you just forgot to add

 new

keyword. This code

objectname(
"11",
"12")

has window object as

 this

so actually when you create object 2 it overrides values from object1.

try

new objectname(
"11",
"12")

returning to the problem of your loop

 objectname 

is just a function, you won't get expected propertieds from it. if you want to get all instances try

 var instances = []
 instances.push(new objectname("1","2"))
 instances.push(new objectname("3","4"))
 for(var i in instances)
     document.write(instances[i].prob1)
Gena Moroz
  • 929
  • 5
  • 9