2017-09-22 36 views
0

私の目標は、隠された、動的にno_parent、名前などのプロパティを設定することですが、それは私を与え続けて:がオブジェクトに動的にプロパティを設定できません

TypeError: Cannot set property 'name' of undefined

私はパラメータで渡す前にscorcrootを初期化した場合でも。ここで

はコードです:

adattamento: function(data) { 
    var continua = true; 
    var scorcfat = this.famiglia; 
    var scorcroot = {}; 
    this.controlloprimi(scorcroot, scorcfat); 
    this.root = scorcroot; 
    console.log("hey!") 
    console.log(this.root); 
    }, 
    controlloprimi: function(scorcroot, scorcfat) { 
    scorcroot.name = scorcfat.name; 
    scorcroot.hidden = false; 
    scorcroot.no_parent = true; 

    if (scorcfat.father != null) { 
     if (scorcfat.mother != null) { 
     scorcroot.children = [{}, {}, {}]; 
     this.controlloprimi(scorcroot.children[1], scorcfat.father); 
     scorcroot.children[2].name = ""; 
     scorcroot.children[2].hidden = true; 
     scorcroot.children[2].no_parent = false; 
     this.controlloprimi(scorcroot.children[3], scorcfat.mother) 
     } else { 
     scorcroot.children = [{}] 
     this.controlloprimi(scorcroot.children[1], scorcfat.father); 
     } 
    } 

    if (scorcfat.mother != null) { 
     scorcroot.children = [{}, {}]; 
     this.controlloprimi(scorcroot.children[1], scorcfat.mother); 
    } 
    }, 
+0

ていることを確認してください 'scorcroot.children [2 ] 'は未定義ではありません。ちょうどポインタ、JSのインデックスは0から始まらず、1から始まる – Rajesh

+0

はい問題は数字です...ありがとう – ImFireblade

答えて

0

scorcroot.children[3]あなただけの3つのオブジェクトでscorcroot.children配列を初期化してきたようにオブジェクトではありません。したがって、scorcroot.children [3]はで、定義されていないのはで、未定義のプロパティを設定しています。

+0

ありがとうございます – ImFireblade

+0

こんにちは@ImFirebladeはあなたのためにこの作品があれば教えてください。それは動作します –

+0

ありがとう、ありがとう! – ImFireblade

0

あなたの問題は、あなただけのこの配列の初期化の3つの項目

scorcroot.children=[{},{},{}]; 

を与えているので、それはscorcroot.children[3]が目的わからundefined

ていないことを意味し、このライン

this.controlloprimi(scorcroot.children[3],scorcfat.mother) 

のようですこのコードでは、3つではなく4つのアイテムにすることをお勧めします。

scorcroot.children=[{},{},{},{}]; 
+0

Omgそんなに愚かな... – ImFireblade

0

問題は は、だから私は3つのオブジェクトの配列を作成し、[3]私は第四未定義のオブジェクトを呼び出した配列を呼び出すことで...配列の間違って列挙した

関連する問題