2012-11-08 4 views
0

내 웹 응용 프로그램에서 사용할 변수를 저장할 개체 만들기를 원합니다.개체 및이 속성에 대한 속성

this을 사용하여 clientIdclientSecreturiGetToken에서 액세스 할 수 없습니다.

또한 의 기능을 token에 사용할 수 있습니다.

내가 뭘 잘못하고 어떻게 수정하는지 말해 줄 수 있습니까?

$(document).ready(function() { 

     // General Settings 
     var mApiSettings = { 
      clientId: 'aaa', 
      clientSecret: 'bbb', 
      token: mApiGetToken(), 
      uriGetToken: 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + this.clientId + '&client_secret=' + this.clientSecret 
     } 

     console.log(mApiSettings.uriGetToken); 
     // Get Autheticated, it requires getting a Token from HollyByte 
     function mApiGetToken() { 

      $.getJSON(mApiSettings.uriGetToken, processData); 
      function processData(data) { 
       mApiSettings.token = data.access_token; 
      } 
      //return token; 
     } 

     // For Testing 
     console.log(mApiGetToken()); 

    }); 
+0

가능한 중복 [? AJAX 응답 텍스트를 반환하는 방법 (http://stackoverflow.com/questions/1225667/how-to-return-ajax : 이런 경우, 당신은이 코드를 사용할 수 있습니다 -response-text) – Quentin

+0

미안하지만 귀하의 링크가 내 질문과 관련이 없으며 속성에 문제가 있습니다. – GibboK

+0

질문에 대답하는 동안 당신은 아직 그 문제를 발견하지 못했습니다. . – Quentin

답변

2

this의 값은 그 함수가 호출 될 때을 나타나는 함수가 결정된다.

this을 사용하는 개체 리터럴과 this의 값 사이에는 연결이 없습니다.

개체 리터럴 문을 생성하는 도중에 개체의 속성에 액세스 할 수있는 방법이 없습니다.

변수를 사용해야합니다.

예제에 특수 문자가 없더라도 URI에 삽입하는 데이터를 이스케이프 처리해야합니다.

var clientId, clientSecret, mApiSettings; 
    clientId = 'aaa'; 
    clientSecret = 'bbb'; 
    mApiSettings = { 
     clientId: clientId, 
     clientSecret: clientSecret, 
     token: mApiGetToken(), 
     uriGetToken: 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + encodeURIComponent(clientId) + '&client_secret=' + encodeURIComponent(clientSecret) 
    } 
+0

내 스크립트를 수정할 수 있습니까? – GibboK

+0

나는 내 대답에 대한 새로운 수정을했다. 코드를 참조하십시오. – AlexStack

+0

마지막 편집을 해 주셔서 감사합니다. 왜 URL에서 문자를 이스케이프 처리합니까? 덕분에 – GibboK

1

this 키워드 ++ Java 또는 C와 같은 다른 OOP 언어처럼 행동하지 않기 때문에 일반적인 자바 스크립트 질문입니다.

문제는 :

var o = { 
    a : 2, 
    b : this.a *2 
} 
console.log(b); //prints NaN because the value for this.a is undefined at the time b is being initialized 

this 때문에 객체 리터럴 초기화 내부에 액세스 할 수 없습니다. 해결 방법은 개체의 이름 대신 this 사용하는 것입니다 :

var o = { 
    a : 2, 
    b : o.a *2 
} 
console.log(b); //prints 4 

을 또는 한 번에 객체를 한 조각을 정의 할 수 있습니다 : 당신이 변경되면

var o = { 
    a : 2 
} 
o.b = o.a *2; 
console.log(b); //prints 4 

어쨌든 코드가 작동합니다 현재 자바 스크립트 this 키워드에 대한 자세한 내용을보실 수 있습니다

var mApiSettings = { 
      clientId: 'aaa', 
      clientSecret: 'bbb', 
      token: mApiGetToken() 
} 

mApiSettings.uriGetToken = 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + mApiSettings.clientId + '&client_secret=' + mApiSettings.clientSecret; 

: mApiSettingshttp://unschooled.org/2012/03/understanding-javascript-this/

편집 :

다른 대답은 당신이 URL로를 삽입하기 전에 clientSecret 및 클라이언트 ID를 인코딩 할 수 있듯이.

var mApiSettings = { 
      clientId: 'aaa', 
      clientSecret: 'bbb', 
      token: mApiGetToken() 
} 

mApiSettings.uriGetToken = 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + encodeURIComponent(mApiSettings.clientId) + '&client_secret=' + encodeURIComponent(mApiSettings.clientSecret); 
+1

"변경하면 코드가 작동해야합니다."- 아니,하지 않아야합니다. 생성 될 때까지 객체 리터럴이 'mApiSettings'에 할당되지 않았기 때문에'Uncaught TypeError : undefined의 clientId '속성을 읽을 수 없습니다.'를 제공합니다. – Quentin

+1

오류를 언급 해 주셔서 감사합니다. 새 코드에서 수정했습니다. – AlexStack

+0

귀하의 편집 및 설명을 해주신 Alex에게 감사드립니다. – GibboK

관련 문제