2012-10-05 39 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