2017-05-24 11 views
0

Iveは割り当て問題があるImです。javascriptオブジェクトの値が期待どおりに動作しない

それは、トラブルシューティングにconsole.log文で次のようになります。

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_node.data $」変数の前と後に返さ得るということです。割当てを示す「色」がオブジェクト「the_node」内に割り当てられていない。

これは何が起こっているのですか?

(以下、オブジェクトとオブジェクトのthe_node」の内容は、「データ」です)

$$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 
+0

あなたのコードは私にとって期待通りに機能します - 実行可能な例を作れますか? –

+0

私は問題を再現できません - http://jsbin.com/motezewi/1/1/edit?js,output - あなたがする結果を得ることができます。私は**新しい*値が設定されて前後に表示され、** duplicates(https://stackoverflow.com/questions/23429203/weird-behavior)という**結果を**別の方法で得ることを理解できました。 -with-objects-console-log)を実行します。 – Quentin

+0

はいその非常に奇妙なことは意味がありません – haz

答えて

0

実際にラインとして含まれているものをthe_node明らかではない。

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

ができません上記の行が正しい場合:

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

正しいですか?

試してみてください。

console.dir(the_node); 

これはthe_nodeの全内容は、メンバーを含む表示されます。

も入力します。

console.log(typeof the_node); 

を、それがオブジェクトであるかどうかを確認するには。

注:変数に$を付加すると、jquery 'color'が追加の機能を多く持つオブジェクトになると、$ colorに割り当てることはできません。$ color.valueやこれから割り当てるオブジェクトの一部です。

+0

the_node.data ["$ color"]は文字列で、the_nodeはオブジェクト – haz

+0

です。あなたのコメントでは、 'the_node'に配列を割り当てます。実際には配列かsome_arrayです。 「データ」と呼ばれるメンバーとは? – SPlatten

+0

はいthats正しい - データ ' – haz

関連する問題