2012-10-05 3 views
0

에서 함수를 사용하는 방법 다음 코드 나던 작업 :자바 스크립트 : 내가 생성자

function CClass() { 
    myFunc(); //says it doesnt know about myFunc 
} 

CClass.prototype = { 
    myFunc: function() {} 
}; 

감사합니다!

+0

나는이 스 니펫이 기대하는 바를 실제로 이해하지 못합니다. 이걸 좀 더 설명해 주시겠습니까? – Chris

+0

정확히 달성하려는 것은 무엇입니까? – fcalderan

답변

0
CClass.prototype.myFunc = function(){ 
    alert("myFunc") 
}; 

function CClass() { 
    this.myFunc(); 
} 

c = new CClass() 
​ 
1

myFunc는 전역 범위에 있지 않습니다.

당신은이 작업을 수행하려고

function CClass() { 
    this.myFunc(); 
} 
0

사용해야합니까?

function CClass() { 
    this.myFunc() 
} 

CClass.prototype = { 
    myFunc: function() { 
     alert('hello from prototype'); 
    } 
}; 


var cc = new CClass(); // hello from prototype 
관련 문제