2014-11-01 3 views
0

I 다음 JS 구조를 가지고자바 스크립트 개체 변수?

var master = { 
    one: 'this is text', 
    two: [ 
     { 
      child: '#mydiv' 
      child_two: '' 
     }, 
     { 
      child: '#mydiv' 
      child_two: '' 
     }, 
    ] 
}; 

는 것이 가능 child_twochild 값과 동일하게이? 이 같은

뭔가 : master.two.child = '#mydiv'

+4

먼저 JS 구조가 합법적 인 자바 스크립트가 아닙니다. 'two'가 배열이 아닌 객체가되어야합니까? – jfriend00

답변

1

아니, 당신은 다음과 같이 두 단계를해야 할 것이다 :

var master = { 
 
    one: 'this is text', 
 
    two: { 
 
     child: '#mydiv' 
 
    } 
 
}; 
 
master.two.child_two = master.two.child; 
 

 
alert(JSON.stringify(master, null, 4));

또는 대안을, 당신은 정의 할 수 있습니다 동일한 값에 대해 미리 변수 :

var myDivSelector = '#mydiv'; 
var master = { 
    one: 'this is text', 
    two: { 
     child: myDivSelector, 
     child_two: myDivSelector 
    } 
};