Empty object javascript

Asked

Viewed 64 times

0

Hello

I have a function that returns an Object with several functions inside, I need this object to be returned as string, but when I do the JSON.stringfy() it returns an empty object.

Can someone help me!

Follow the Code below:

function run() {
    var start = ({

                "dinamico": function(opcao, params) {

                        var options = {

                            quickplies: (function(params){
                                    
                                var dinamico = {

                                            "type": "application/vnd.lime.select+json",
                                            "content":{
                                                "scope":"immediate",
                                                "text": params.title,
                                                "options": params.options 
                                            }
                                        }

                                    return dinamico

                            })(params)
                        }

                        return options[opcao]
                }
    })

    return JSON.stringify(start)
}

var funcoes = run();
console.log(funcoes)
  • 1

    JSON.stringify does not serialize functions (its object start has only one property, which is a function). Thus, since JSON does not support functions, it will not be "placed" in the output of JSON.stringify.

  • Edith your question to remove the code in image. Put the formatted code as text between \```. As you can see here, post code as image nay is a good practice on this site.

  • there is some way to export an object with functions inside as string?

  • 1

    It even exists, but the reverse conversion can open up a relatively serious security breach (XSS). I would avoid this as much as possible. Why do you want to serialize the object?

  • I am using blip to build chat bots, in it it is possible to create dynamic content such as carousel images menu etc.. i want to leave a global object exposed so that when I need to create some of these contents I just call the specific function of that object.

Show 1 more comment

1 answer

0

Jailson did not quite understand why of this, however. The way he is declaring the 'start' object is not common. Unable to use stringify in functions, but if you want to proceed:

function run() {
var start = ({

            "dinamico": function(opcao, params) {

                    var options = {

                        quickplies: (function(params){
                                
                            var dinamico = {

                                        "type": "application/vnd.lime.select+json",
                                        "content":{
                                            "scope":"immediate",
                                            "text": params.title,
                                            "options": params.options 
                                        }
                                    }

                                return dinamico

                        })(params)
                    }

                    return options[opcao]
            }
})

return ''+ start.dinamico;
}

var funcoes = run();
console.log(funcoes)

The top has a code that I believe will do what you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.