2012-10-05 41 views
0

使用函數下面的代碼不工作:JavaScript的:我怎樣才能在一個構造

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