So if I declare an object in Javascript like so:
var myhouse = {
room: {
bed: function() { return "sleep"; }
}
}
I can call:
myhouse.room.bed();
Can I make a method that’s callable with just:
myhouse.room();
? I can’t just say:
var myhouse = {
room: {
function() { return "stuff"; },
bed: function() { return "sleep"; }
}
}
It’s invalid. So what am I missing? Is it even a good idea to do this?
You can’t do it with a single literal, you need an extra assignment to set a property on a function:
To make it a single expression, you could either use a IEFE
or the comma operator (horribly unreadable, but working):