简体   繁体   English

强制传递一个匿名函数,该函数将对象返回为javascript中的参数

[英]Enforce passing a anonymous function that returns an object as a parameter in javascript

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: 有时我忘记做最后一条语句aFunction(x),这使我很头疼,所以我做了以下工作:

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. 哪个可行,我很好奇我的函数方法aFunction中是否有一种方法强制传递的参数必须是匿名函数。 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: 我知道我可以执行以下操作,并且上面的检查将起作用,但是我想将所有逻辑放在括号内的Function参数中,如代码片段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. 我不能像现在那样立即调用匿名函数,而不能在aFunction方法中调用它。 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'); 
   }

} }

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. 我很好奇我的函数方法aFunction是否有一种强制执行的方法,即所传递的参数必须是匿名函数。

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 that x is a function and then call it to get the value/object to work with: 您可以传递一个函数(而不是调用函数的结果),然后在aFunction(x)测试x是一个函数,然后调用它以获取要使用的值/对象:

function aFunction(x) {
    if(typeof x === "function") {
        var workingValue = x();
    }
}

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. 您的2号语法就可以了,它的优点是将任何工作变量都保留在当前范围之外(假设不会破坏某些内容),但是要达到您确定的目标,即确保您不要忘记调用aFunction()在其他处理结束时,嗯... 我的看法是,这只会使其他人难以阅读代码。

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. 我认为您的原始数字1版本是最好的方法,或者,如果您必须使用某个功能,那么数字2版本也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM