I have a function and an object inside that function. I am returning the object. How can I call the function and then get the obj.a in console.log();?
function _() {
var obj = {};
obj.a = "one";
obj.b = "two";
return obj;
}
console.log(_);
Simply execute the function (_(), which will return an object), then access its inner properties using dot notation as you normally would:
function _() {
var obj = {};
obj.a = "one";
obj.b = "two";
return obj;
}
console.log(_().a);
console.log(_().b);
You may want to consider storing the result of executing the function as a separate variable first though, rather than executing it repeatedly every time you wish to access a property from the object it returns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With