0

i am having a problem that i am finding difficult to find information about as i am unaware of the underlying issue. I am trying to set a property inside an object, and when i console.log the property it gives the anticipated result but when i console.log the entire object, the property inside is different.

defaults:{
        uid:undefined,      
        createSphere:function(uid,coordinates)
        { 
        this.uid = uid;
        console.log(this.uid);
        console.log(uid);
        console.log(this);
        console.log(this.uid);

I run the createSphere function in a simple for loop. Here you can see how i assign the uid for the function.

for(i = 0;i<n;i++)
        {
        coordinates = {x:0,y:5,z:0}
        coordinates.x = 0+ (40*Math.sin(Math.PI*(i/2)))
        coordinates.z = 0+ (40*Math.cos(Math.PI*(i/2)))
        spheres.defaults.createSphere((i+1),coordinates);
        }

Here you can see the resulting log when creating the first sphere with the code from the first block. The console.logs are executed directly after eachother, so nothing is able to change the value between the logging. I wanted to upload it in an image for better clarity but i unfortunately cannot.

1                                         
1                                          
Object
action: function (order,impulseVector){
createSphere: function (uid,coordinates)
  uid: 2
__proto__: Object
1

So the problem is; when taking the value directly from the property it differs from when using the entire object.

Sigismundus
  • 635
  • 4
  • 15
  • “_The console.logs are executed directly after eachother, so nothing is able to change the value between the logging._”—I wouldn’t rely on that. `console.log()` is often “too slow”, because it doesn’t log a variable or property as it is in exactly that moment but only takes the reference of that variable and evaluates it at some point _later_ where the value may have long been changed. – Sebastian Simon Mar 12 '15 at 00:44
  • Post running code demonstrating this behavior using Code Snippet icon. It supports JavaScript. – PM 77-1 Mar 12 '15 at 00:58
  • It's more than 800 lines and it uses alot of libaries and resources unfortunately. I noticed that all the created objects share the uid of the _last_ created sphere. Does this mean it is not creating a new object, but instead overwriting the old one? – Young-Chul Lim Mar 12 '15 at 01:02
  • I think this is either the classic __scope/closure__ question (wrap your loop in an anonymous function) or a dupe of the [lazy log problem](http://stackoverflow.com/questions/4057440). – Evan Davis Mar 12 '15 at 02:00

1 Answers1

0

When you call:

spheres.defaults.createSphere((i+1),coordinates);

It isn't creating a new sphere object. Instead it is only updating "spheres.defaults.uid".

Perhaps this is closer to what you are looking for:

var Sphere = function(uid, coordinates){
  this.uid = uid;
  this.coordinates = coordinates;
  }

var spheres = [];

spheres.push(new Sphere(1, null));
spheres.push(new Sphere(2, null));
spheres.push(new Sphere(3, null));
spheres.push(new Sphere(4, null));

console.log(spheres);
Seamus
  • 4,539
  • 2
  • 32
  • 42