0

Ive a function that Im having an assignment issue with.

It looks like this with the troubleshooting console.log statements:

function myfunc()
    {

  the_node = some_array; //with data.$color = #111111

    console.log('before: '+the_node.data["$color"]); //returns #111111
    console.log(the_node); //returns #111111 (at data.$color)

    the_node.data["$color"] = "#000000" ; //the assignment

    console.log('after: '+the_node.data["$color"]); //returns #000000
    console.log(the_node); //returns #111111 (should return #000000) (at data.$color)

  }

The interesting thing is that on the console I get the right values returned for before and after for the variable 'the_node.data.$color' showing the assignment has taken place but it has not been assigned within the object 'the_node'.

Any ideas why this is happening?

(below are the contents of the object 'the_node' with the object 'data')

$$family: "class"

Config: {$extend: false, overridable: true, type: "multipie", color: "#e6e6e6", alpha: 1, …}

Edge: {$extend: false, overridable: false, type: "none", color: "#ccb", lineWidth: 1, …}

Label: {$extend: false, overridable: true, type: "Native", style: " ", size: 10, …}

Node: {$extend: false, overridable: true, type: "multipie", color: "#e6e6e6", alpha: 1, …}

_angularWidth: 1

_depth: 1

_flag: true

_treeAngularWidth: 3.1666666666666665


angleSpan: {begin: 0, end: 2.7762911822421428}

constructor: function()

data: Object
$alpha: "1"
$color: "#87b13e"
$dim-quotient: 1
$label-size: 15
$span: 2.7762911822421428
class: "trait"
color: "#000000"
trait: "endurance"

Object Prototype
haz
  • 740
  • 1
  • 11
  • 20
  • your code works as expected for me - can you make a runnable example – Jaromanda X May 24 '17 at 10:23
  • I can't reproduce the issue — http://jsbin.com/motezewihe/1/edit?js,output — or see any way that you could get the result you do. I could understand getting the results **the other way around** with the *new* value showing up before and after it was set and there are [duplicates](https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log) for that. – Quentin May 24 '17 at 10:23
  • yes its quite strange .. makes no sense – haz May 24 '17 at 10:24
  • can you provide the array (the_node) so that we can have a better understanding? – Bidisha Pyne May 24 '17 at 10:45
  • pls see obj explanation – haz May 25 '17 at 06:47

1 Answers1

0

It isn't clear what the_node actually contains as the line:

     console.log(the_node); //returns #111111

Cannot be correct if the line above:

     console.log('before: '+the_node.data["$color"]); //returns #111111

Is correct?

Try:

    console.dir(the_node);

This will cause the full contents of the_node to be displayed including members.

Also type:

    console.log(typeof the_node);

To see if it is an object.

NOTE: When prefixing any variable with $, if using jquery 'color' becomes an object with lots of additional functionality, you cannot then simply assign to $color, you should be assigning to something like $color.value or which ever part of the object you want to assign to.

SPlatten
  • 5,334
  • 11
  • 57
  • 128