-1
我一直在用javascript工作了一段时间,但从未做过任何类,我的工作已经越来越杂乱无章的功能吨。所以现在我正在学习如何使用类,我有问题。Javascript学习课程问题
我怎么能执行一个类,例如内功能:
<html>
<head>
<script type="text/javascript">
window.onload = function(){
var object = new testClass("test123");
alert(object.content);
}
function testClass (id){
this.object = document.getElementById(id);
this.content = this.getContent(); //<- this is what I want to do
this.getContent = function(){
return this.object.innerHTML;
}
}
</script>
</head>
<body>
<div id="test123">Hello World</div>
</body>
</html>
基本上怎么能我呼吁函数类的内部类里面呢?在这个例子中,我怎么能得到this.getContent
返回"Hello World"
。
我知道我能做到这一点,如:
<html>
<head>
<script type="text/javascript">
window.onload = function(){
var object = new testClass("test123");
alert(object.getContent());
}
function testClass (id){
this.object = document.getElementById(id);
this.getContent = function(){
return this.object.innerHTML;
}
}
</script>
</head>
<body>
<div id="test123">Hello World</div>
</body>
</html>
我使用Chrome测试。我做错了,能做到吗?提前致谢。