The following code below is broken all it needs to work is var thing = new thing();, I understand this. My question is is there a way for the below code to work without declaring new? Something in the thing or something objects?
var thing = function(){
var something = {};
something.num = function(){
return 5;
};
return something;
};
console.log(thing.num());
I believe you’re looking for this:
This will just execute that function immediately, and assign the return value (
something) tothing. Your original code assigned the function itself tothing. If you want to keep the original function, you can useconsole.log(thing().num()).