This is a general programming question, not specific to this code example.
How do I detect whether a particular method was called or not from within a function?
Imagine you want to be able to tell whether a particular method was called so that you can change what you return based on that.
var turtle = function() {
var vars = {
name: "Shelly",
age: 103
};
this.actions = function() {
var methods = {
crawl: function() {
//etc...
},
mutate: function() {
//etc...
},
eat: function() {
//etc...
}
};
if (methodWasCalled) { // <- The part I don't know how to do
return methods;
} else {
return this;
}
};
if (methodWasCalled) { // <- The part I don't know how to do
return vars;
} else {
return this;
}
};
Based on the above example, someone could use: turtle() and get access to the 'vars' object, or they could use: turtle().mutate() and the turtle object would execute the block inside that function.