I have the following code
1) var x = {};
x.data = {name: 'name', lname: 'lname'};
x.someOtherData = ['blah','blah','irrelevant'];
aFunction(x);
So I call the above group of statements several times, order of the statements being important ofcourse. I sometimes forget to do the last statement aFunction(x) which has caused me a lot of headaches, so I’ve done the following:
2) aFunction(function(){
var x = {};
x.data = {name: 'name', lname: 'lname'};
x.someOtherData = ['blah','blah','irrelevant'];
return x;
}());
Which works and I was curious if there was a way of enforcing in my function method, aFunction, that the parameter being passed must be a an anonymous function. I am returning an object from that anonymous function so the following will obviously not work
3) function aFunction(x) {
if(x.constructor == Function) {
alert('yay you're doing it right');
} else {
alert('nay, you're doing it wrong'); //this is what happens given that x.constructor == Object
}
}
I know I can do the following and the above check will work, but I would like to enclose all my logic inside aFunction argument, in the parenthesis, as in code snippet 2:
4) var z = function(){
var x = {};
x.data = {name: 'name', lname: 'lname'};
x.someOtherData = ['blah','blah','irrelevant'];
return x;
};
aFunction(z);
Any ideas?
Update:
Nevermind I figured it out. I can just not call the anonymous function immediately as I did and call it in aFunction method. Sure helps to write out your problems in stack overflow so that the problem and solution is clearer. Still open to how others solve similar problems. Design patterns etc..
Solution:
aFunction(function(){
var x = {};
x.data = {name: 'name', lname: 'lname'};
x.someOtherData = ['blah','blah','irrelevant'];
return x;
});
function aFunction(x) {
if(x.constructor == Function) {
alert('yay you're doing it right');
x(); //call it here
} else {
alert('nay, you're doing it wrong');
}
}
But you are not passing an anonymous function. You are calling an anonymous function and passing the result.
You could pass a function (rather than the result of calling a function), and then within
aFunction(x)you’d test thatxis a function and then call it to get the value/object to work with:But of course that doesn’t in any way ensure that the function passed in will return a value in the right format. Also it doesn’t ensure that it is an anonymous function.
Your number 2 syntax is fine as is, with the advantage of keeping any working variables out of the current scope (assuming that doesn’t break something), but as far as your stated goal of making sure you don’t forget to call
aFunction()at the end of your other processing, well… My opinion is it just makes the code harder for others to read.EDIT: Just saw your updated question, where you’ve decided to do what I mentioned above (seems you were updating at the same time I was answering). Seriously, I really think this is a bad design pattern, and I only really mentioned it as a hypothetical solution – I don’t think I made it clear initially that I don’t actually recommend it.
In my opinion your original number 1 version is the best way, or if you must use a function your number 2 version is OK.