Is it valid to define functions in JSON results?

后端 未结 11 949
一向
一向 2020-11-22 11:38

Part of a website\'s JSON response had this (... added for context):

{..., now:function(){return(new Date).getTime()}, ...}

Is adding anony

11条回答
  •  再見小時候
    2020-11-22 11:56

    Via using NodeJS (commonJS syntax) I was able to get this type of functionality working, I originally had just a JSON structure inside some external JS file, but I wanted that structure to be more of a Class, with methods that could be decided at run time.

    The declaration of 'Executor' in myJSON is not required.

    var myJSON = {
        "Hello": "World",
        "Executor": ""
    }
    
    module.exports = {
        init: () => { return { ...myJSON, "Executor": (first, last) => { return first + last } } }
    }
    

提交回复
热议问题