2013-03-12 5 views
3

일 때 Javascript 문이 쉼표 또는 세미콜론으로 끝납니다. 자바 스크립트 OO에 익숙하지 않습니다. 몇 가지 실험을 한 후에 오브젝트 정의에 대한 약간의 혼란스러운 질문이 있습니다. 아래 코멘트. 고맙습니다.오브젝트 정의가

GlobalUtility = function() { 
      this.templeteCode = "123"; 

      this.Init(); 
//why can not put this code here, 
//there is an error says GetOriginalSource is not a function . 
//This is not like the classical OO constructor which can call any methods in class. 
      this.Init = function() { 
       var source=this.GetOriginalSource(); 
       alert(source + " is ok."); 
      },//I found I can end this statement by , or ; Is there any difference between them? 

      this.GetOriginalSource=function(){ 
       return "abc"; 
      }; 
      //this.Init(); putting this code here is ok . 

     }; 
+1

나는이 질문 codeReview' 또는'Programmers''로 이동 될 필요가 있다고 생각 재미 없습니다. – Red

+0

안녕하세요, @ Red, codeReview 및 Programmers는 어디 있습니까? 제게 링크를 제공해 주시겠습니까? 다음에 나는 그것을 따라갈 것이다. 감사합니다. –

+0

http://programmers.stackexchange.com/?as=1, http://codereview.stackexchange.com/?as=1 또한 놀라운 스택 사이트를 확인하십시오. – Red

답변

3
  1. 함수를 호출하기 전에 먼저 함수를 정의해야합니다.
  2. 자바 스크립트의 세미콜론은 선택 항목입니다. 기본적으로 쉼표는 개체로 작업하는 동안 세미콜론은 명령문을 끝내기 위해 사용됩니다. 당신은이 기사 JavaScript variable definition: Commas vs. Semicolons 및 읽기 시도 할 수 자바 스크립트가 * defining javascript class를 참조 OOP 방식으로 기록 할 수 있지만 내가 Base.js를 사용하는 것이 좋습니다 Do you recommend using semicolons after every statement in JavaScript?

, 그것은 여러분의 인생을 더 쉽게 만드는 것입니다.

당신은 아마이 필요하지만 읽기 : javascript patterns

+0

코드를 압축하는 경우 세미콜론 (;)을 사용하는 것이 좋으므로 코드가 다른 것으로 변할 수 있습니다 (예 : var foo = 1var bar = 2; 또한 압축 할 때 주석의 //를 피하십시오. – Hiny

0

this.GetOriginalSource=function(){이 개체에 기능을 추가합니다. 이 선 앞에는 있지 않습니다. 그러나 전에 전화하려고합니다.

GlobalUtility = function() { 
      Init(); 


      this.templeteCode = "123"; 
      this.Init = Init;  
      this.GetOriginalSource = GetOriginalSource; 

      //function declaration 
      function Init() { 
       var source = GetOriginalSource(); 
       alert(source + " is ok."); 
      } 
      function GetOriginalSource() { 
       return "abc"; 
      } 
}; 

아직 런타임에 정의되지 않은 함수를 호출하려는 :

0

이 시도.