<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>

解决方案 »

  1.   

    你的getCurrent 是 newObject的属性方法
    如果你不return 一个 newObject 对象,new CurrentAnswer 得到的将是CurrentAnswer 对象,不能调用newObject 的属性方法
      

  2.   

    由楼主的问题想到的:
    我这里想访问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());
      

  3.   


    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());    .
      

  4.   

    楼主定义的current是方法内部的一个局部变量,方法返回后是无法访问的。
      

  5.   


    照你那么说,又有了一个新疑问,CurrentAnswer还可以返回别的对象?你的意思是可以在CurrentAnswer对象中return 一个 newObject 对象?
      

  6.   

    是的,其实任何语言都可以 ,这里相当于声明了(如果是在强类型语言里的话)
    public newObject CurrentAnswer(){
    //  doStg..return newObject ;
    }
    强类型语言里这个叫做内部类