newPage.getName();
既然知道了newPage,为什么还要再返回给你呢?不解

解决方案 »

  1.   

    function MyPage(_name){
        // 成员变量
        this.name = _name;
        this.pageSize = 10;
        this.lineNum = 20;
        // 方法
        this.getName = _getName;
    }function _getName(){
        return this._name;
    }var newPage = new MyPage("newPage");
      

  2.   

    其实我希望实现的功能是:
    定义一个新对象,在这个对象中创建多个checkbox, 并给新创建的
    checkbox attach一个onclick事件,响应事件是这个对象中的一个函数,
    由于将这个checkbox插入到document的body中后,onclick事件触发这个
    函数后,这个函数并不属于这个对象,比如:
    function MyPage(){
        this.checkBoxState = new Array();
        this.createCheckBox = _createCheckBox;
        this.chooseCheckBox = _chooseCheckBox;
    }function _createCheckBox(){
        var newCheckBox = document.createElement("<input type=\"checkbox\">");
        newCheckBox.value = 1;
        newCheckBox.attachEvent("onclick", this.chooseCheckBox());
    }function _chooseCheckBox(){
        var srcObj = window.event.srcElement;
        var value = parseInt(srcObj.value);
        this.checkBoxState[value] = (srcObj.checked) ? 1 : 0;
    }
    当选择checkBox时,调用函数chooseCheckBox时有错误,因为这时
    this对象不是这个对象,也就是说触发这个函数时没有把对象传递到
    函数中。
    因此我想如果在对象的函数中能够取得这个对象的名称,那么在相应
    事件的函数中就可以访问用户定义的对象了,而且我希望在一个页面
    中可以定义多个这种自定义对象,另外我不希望在定义这个对象的时
    候还需要将对象的名称传给构造函数。
      

  3.   

    那按照我的理解,就不是取
    var newPage = new MyPage();
    的name(newPage )。而是取
    var newCheckBox = document.createElement("<input type=\"checkbox\">");
    的name。