Javascript calling eval on an object literal (with functions)

前端 未结 2 1894
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 04:51

Disclaimer: I fully understand the risks/downsides of using eval but this is one niche case where I couldn\'t find any other way.

In Google Apps Scripting, there sti

2条回答
  •  無奈伤痛
    2020-12-11 04:57

    You cannot evaluate

    {
      fetchDate: function() {
        var d = new Date();
        var dateString = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
        return dateString;
      }
    }
    

    Because it is not a valid expression (Object literals on their own are interpreted as blocks. fetch: function () { } is not a valid expression).

    Try

    var myLibName = {
      fetchDate: function() {
        var d = new Date();
        var dateString = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
        return dateString;
      }
    };
    

提交回复
热议问题