<html><head><title></title></head><body>
<script language="javascript" type="text/javascript">
/*用类实现这样的作用
function CurrentAnswer(num)
{
var current=num;
return "the currrnt answer is :"+current;
}
alert(CurrentAnswer(5));
*/
function CurrentAnswer(num)
{
var current=num;
var newObject={
getCurrent:function ()
{
return "the currrnt answer is :"+current;
}}
return newObject;//为什么一定有这句话呢?
}var curr=new CurrentAnswer(5);
alert(curr.getCurrent());</script>
</body></html>
<script language="javascript" type="text/javascript">
/*用类实现这样的作用
function CurrentAnswer(num)
{
var current=num;
return "the currrnt answer is :"+current;
}
alert(CurrentAnswer(5));
*/
function CurrentAnswer(num)
{
var current=num;
var newObject={
getCurrent:function ()
{
return "the currrnt answer is :"+current;
}}
return newObject;//为什么一定有这句话呢?
}var curr=new CurrentAnswer(5);
alert(curr.getCurrent());</script>
</body></html>
如果你不return 一个 newObject 对象,new CurrentAnswer 得到的将是CurrentAnswer 对象,不能调用newObject 的属性方法
我这里想访问current怎么办?function CurrentAnswer(num){
this.current = num;
this.newObject = {
getCurrent: function(){
return "the currrnt answer is :" + current;
}
}
}
var curr = new CurrentAnswer(5);
alert(curr.newObject.getCurrent());
newObject 是在 CurrentAnswer里定义的,所以他的作用范围是 CurrentAnswer.
所以在外面是访问不到的,相当于private的想要访问有两个方法1 /*用类实现这样的作用
function CurrentAnswer(num)
{
var current=num;
return "the currrnt answer is :"+current;
}
alert(CurrentAnswer(5));
*/
function CurrentAnswer(num)
{
var current=num;
this.newObject={
getCurrent:function ()
{
return "the currrnt answer is :"+current;
} }
// return newObject;//为什么一定有这句话呢?
}
var curr=new CurrentAnswer(5);
var s=curr.newObject;
alert(s.getCurrent());2. /*用类实现这样的作用
function CurrentAnswer(num)
{
var current=num;
return "the currrnt answer is :"+current;
}
alert(CurrentAnswer(5));
*/
function CurrentAnswer(num)
{
var current=num;
var newObject={
getCurrent:function ()
{
return "the currrnt answer is :"+current;
} }
this.getNewObj=function(){
return newObject;
}
// return newObject;//为什么一定有这句话呢?
}
var curr=new CurrentAnswer(5);
var s=curr.getNewObj();
alert(s.getCurrent()); .
照你那么说,又有了一个新疑问,CurrentAnswer还可以返回别的对象?你的意思是可以在CurrentAnswer对象中return 一个 newObject 对象?
public newObject CurrentAnswer(){
// doStg..return newObject ;
}
强类型语言里这个叫做内部类