使用javascript方法

使用javascript方法,javascript,methods,Javascript,Methods,要获得像“4,3”这样的值,我有以下方法: var value = objeto.row(4).cell(3); 开发js方法的正确形式是什么?我想我必须这样做: function objeto(rowIndex){ cell: function(cellIndex){ return rowIndex + ',' + cellIndex; } } 我知道这不管用,只是猜测而已。谢谢你 如果要保留单元格属于行的层次结构,可以执行以下操作: var objeto = {

要获得像“4,3”这样的值,我有以下方法:

var value = objeto.row(4).cell(3);
开发js方法的正确形式是什么?我想我必须这样做:

function objeto(rowIndex){
   cell: function(cellIndex){
      return rowIndex + ',' + cellIndex;
   }
}

我知道这不管用,只是猜测而已。谢谢你

如果要保留单元格属于行的层次结构,可以执行以下操作:

var objeto = {
    row: function ( rowIndex ) {
        return ( function ( rowIndex ) {
            this.cell = function ( cellIndex ) {
                return rowIndex + ',' + cellIndex;
            };
            return {
                cell: this.cell
            }
        }( rowIndex ) );
    }
}

如果要保留单元格属于行的层次结构,可以执行以下操作:

var objeto = {
    row: function ( rowIndex ) {
        return ( function ( rowIndex ) {
            this.cell = function ( cellIndex ) {
                return rowIndex + ',' + cellIndex;
            };
            return {
                cell: this.cell
            }
        }( rowIndex ) );
    }
}

另一种不向
窗口添加内部函数的方法

var objeto = { 
    row : function (rowIndex){ 
        return { 
            cell : function (cellIndex){ 
                return rowIndex+','+cellIndex; 
            }
        }
    }
}

另一种不向
窗口添加内部函数的方法

var objeto = { 
    row : function (rowIndex){ 
        return { 
            cell : function (cellIndex){ 
                return rowIndex+','+cellIndex; 
            }
        }
    }
}

您是否正在询问如何编写代码,以便使用提供的第一位代码中的语法来获得“4,3”?您必须向objeto原型添加两种方法:
row()
将设置
this.row
值并返回
this
,而
cell()
将返回
this.row+,“+this.cell
。我猜。你只是忘记了
row
,所以你需要这样的东西:
var objeto={row:function(rowIndex){return{cell:function(cellIndex){return rowIndex+','+cellIndex;}}}}}
你是在问如何编写代码,以便在你提供的第一位代码中使用语法来获得“4,3”吗?必须向objeto原型添加两种方法:
row()
将设置
this.row
值并返回
this
,而
cell()
将返回
this.row+,“+this.cell
。我猜。你只是忘记了
row
,所以你需要这样的东西:
var-objeto={row:function(rowIndex){return{cell:function(cellIndex){return rowIndex+','+cellIndex;}}}
此.cell
cell
函数添加到
窗口中(在您的情况下:-)请注意,无论何时调用函数,您都在创建或分配全局变量
cell
。您应该避免这种情况。在row方法中不需要额外的闭包。
rowIndex
的值已经通过row方法保留了下来,row方法本身就是一个闭包。Grundy的答案是首选解决方案。
this.cell
add
cell
函数到
window
在您的情况下:-)请注意,无论何时调用函数,您都在创建或分配全局变量
cell
。您应该避免这种情况。在row方法中不需要额外的闭包。
rowIndex
的值已经通过row方法保留了下来,row方法本身就是一个闭包。格伦迪的答案将是首选的解决方案。