Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何创建值为函数的对象?_Javascript_Google Apps Script - Fatal编程技术网

Javascript 如何创建值为函数的对象?

Javascript 如何创建值为函数的对象?,javascript,google-apps-script,Javascript,Google Apps Script,我需要通过编程创建一个对象,该对象具有一对属性:value liketmpCol[“formatter”]=color,其中color的定义如下 var colors = function backgroundColor(cell, formatterParams){} 一切正常 但是我需要在javascript之外动态地创建对象。我使用Google脚本创建基于数据的列标题定义。我试图在服务器端定义一个伪函数var colors=function backgroundColor(cell,fo

我需要通过编程创建一个对象,该对象具有一对属性:value like
tmpCol[“formatter”]=color
,其中color的定义如下

var colors = function backgroundColor(cell, formatterParams){}
一切正常

但是我需要在javascript之外动态地创建对象。我使用Google脚本创建基于数据的列标题定义。我试图在服务器端定义一个伪函数
var colors=function backgroundColor(cell,formatterParams){}
,这样定义甚至可以通过运行。但当我在浏览器中运行页面时,函数定义在我看来似乎是一个文本,而不是一个函数。浏览器使用
google.script.run
GAS返回定义,调用获取数据

类型定义
tmpCol[“格式化程序”]=颜色工作,但不是气体处理时的代码

服务器端的html代码如下所示

  <script>
  var colors = function backgroundColor(cell, formatterParams){
    var value = cell.getValue();
    if ("color" in formatterParams) {
      var color = cell.getRow().getData()[formatterParams["color"]];
      if (color){
        cell.getElement().style.backgroundColor = cell.getRow().getData()[formatterParams["color"]];
      }
    }
  return value;
  }
      var initialTableData = [{id:0,name:"nahrávají se data"}];
      var table = new Tabulator("#zz-test", {
          layout:"fitColumns",
          responsiveLayout:"hide",
          data:initialTableData,
          dataTree:true,    
          selectable:true,
          columns:[
            {title:"",
              field:"name",
              headerSort:false,
            },
          ],
      });
google.script.run.withSuccessHandler(updateTable).callLibraryFunction("zzlib.getTabulatorData",'A87J8HRS1');
      
  function updateTable(data){
    table.deleteColumn("name");
    for ( var c = 0; c < data.tableColumns.length; c++){
      table.addColumn(data.tableColumns[c]);
    }
//test
      var tmpCol = {};
    tmpCol["title"]="test 1";
    tmpCol["field"]="test1";
    tmpCol["headerSort"]=false;
    tmpCol["formatter"]=colors;
    tmpCol["formatterParams"]={color:"color1"};    
  
  columns = [
        {title:"", field:"name", width:200},
        tmpCol,
        {title:"Test2", field:"test2", width:200,formatter:colors,formatterParams:{color:"color2"}}
        ];

  //  table.deleteColumn("name");
  //  table.deleteColumn("test1");
  //  table.deleteColumn("test2");

    for ( var c = 0; c < columns.length; c++){
     // table.addColumn(columns[c]);
    }
console.log(columns);
// test
    table.setData(data.tableData); 
  }
    </script>
知道如何在服务器端创建对象值并将其正确传递给浏览器吗?

假设您有:

var functionString = "function backgroundColor(cell, formatterParams){}";
您可以使用
函数
构造函数:#推荐

var colors = new Function("return " + functionString)()
这解析了匿名函数中的函数,我们添加了
return
,因此通过调用匿名函数我们可以得到目标函数,最后我们添加了调用
()

例如:

var functionString=“function backgroundColor(cell,formatterParams){return'Hey!'}”;
var anonymousFunction=新函数(“返回”+函数字符串);
var targetFunction=anonymousFunction();
console.log(匿名功能);
log(匿名函数());
console.log(targetFunction);
log(targetFunction())假设您有:

var functionString = "function backgroundColor(cell, formatterParams){}";
您可以使用
函数
构造函数:#推荐

var colors = new Function("return " + functionString)()
这解析了匿名函数中的函数,我们添加了
return
,因此通过调用匿名函数我们可以得到目标函数,最后我们添加了调用
()

例如:

var functionString=“function backgroundColor(cell,formatterParams){return'Hey!'}”;
var anonymousFunction=新函数(“返回”+函数字符串);
var targetFunction=anonymousFunction();
console.log(匿名功能);
log(匿名函数());
console.log(targetFunction);

log(targetFunction())我需要运行代码定义两次。我需要定义两列。它们中的任何一个-function或eval对我都有效。也许我做错了什么。Eval什么都不做,当使用
新函数(“return”+functionString)
表单元格得到的是函数的文本而不是值。很抱歉,我刚刚更新了答案,通过使用函数构造函数,我们需要调用以返回目标函数本身,所以我们只需要在末尾添加
()
,要成为
var colors=new函数(“return”+functionString)(
cool)。这个函数工作于
新函数(“return”+functionString)()新函数(“return”+functionString)
表单元格得到的是函数的文本而不是值。很抱歉,我刚刚更新了答案,通过使用函数构造函数,我们需要调用以返回目标函数本身,所以我们只需要在末尾添加
()
,要成为
var colors=new函数(“return”+functionString)(
cool)。这个函数工作于
新函数(“return”+functionString)()