javascript - Object as a function -


i want have following working, object returned getobject function date. assume done if object in fact function or may there other ways it.

var o; var module = {   setobject: function(newobject) {     o = newobject;   },   getobject: function() {     if(o) {       return o;     }     o = {"a": "123"}     return o   } } var objcopy = module.getobject(); console.log(objcopy.a) module.setobject({"a":"321"}) console.log(objcopy.a) 

as seen setting new object doesn't change objcopy apparently , output "123", "123". ways achieve this? object creator module node.js module used other code.

make o property of module, or object.

var o = { obj: undefined }; var module = {       setobject: function(newobject) {         o.obj = newobject;     },     getobject: function() {         if(o) {           return o;         }         o.obj = {"a": "123"}         return o     } } var objcopy = module.getobject(); console.log(objcopy.obj.a) module.setobject({"a":"321"}) console.log(objcopy.obj.a) 

Comments