JSON inside a closure

1

I have a JSON , for example:

var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';

And I want to insert it inside a closure in JavaScript , but read the JSON from outside the closure , I can not read it from the outside.

var add = (function () {
    var counter = 0;
    return function () {

return var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';
}
    })();

It's an example, even after parsing it I can not get the JSON data, or modify it. Any help?

    
asked by jluisnjose luis narvaez juarez 22.11.2016 в 16:42
source

1 answer

1

It's that return var is wrong ... You have to undock it as follows, and also pause it, to return it as an object.

var add = (function () {
    var counter = 0;
    return function () {
        var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';
        text = JSON.parse(text);
        return text;
    }
})();
    
answered by 23.11.2016 в 04:15