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

发布于 2024-12-25 06:21:28 字数 1628 浏览 2 评论 0原文

我有以下代码

1)  var x = {};
    x.data = {name: 'name', lname: 'lname'};
    x.someOtherData = ['blah','blah','irrelevant'];
    aFunction(x);

,因此我多次调用上述一组语句,当然语句的顺序很重要。我有时会忘记执行最后一个语句 aFunction(x) 这让我很头疼,所以我做了以下操作:

2) aFunction(function(){
    var x = {};
    x.data = {name: 'name', lname: 'lname'};
    x.someOtherData = ['blah','blah','irrelevant'];
    return x; 
   }());                     

哪个有效,我很好奇是否有一种方法可以在我的函数方法 aFunction 中强制执行传递的参数必须是匿名函数。我正在从该匿名函数返回一个对象,因此以下内容显然不起作用

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
   }
}

我知道我可以执行以下操作并且上面的检查将起作用,但我想将所有逻辑包含在 aFunction 参数内,放在括号中,如代码中所示片段2:

4) var z =  function(){
   var x = {};
   x.data = {name: 'name', lname: 'lname'};
   x.someOtherData = ['blah','blah','irrelevant'];
   return x; 
  };
  aFunction(z);  

有什么想法吗?

更新:

没关系,我已经弄清楚了。我不能像我那样立即调用匿名函数并在 aFunction 方法中调用它。当然有助于在堆栈溢出中写出您的问题,以便问题和解决方案更加清晰。对其他人如何解决类似问题仍然持开放态度。设计模式等。

解决方案:

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 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'); 
   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2025-01-01 06:21:28

我很好奇是否有一种方法可以在我的函数方法 aFunction 中强制传递的参数必须是匿名函数。

但是您没有传递匿名函数。您正在调用匿名函数并传递结果。

您可以传递一个函数(而不是调用函数的结果),然后在 aFunction(x) 中测试 x 是否是一个函数,然后调用它获取要使用的值/对象:

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

但是当然,这并不能以任何方式确保传入的函数将返回正确格式的值。它还不能确保它是一个匿名函数。

你的数字 2 语法就很好,优点是可以将任何工作变量保留在当前范围之外(假设不会破坏某些内容),但就你所声明的目标而言,确保你不会忘记调用 < code>aFunction() 在你的其他处理结束时,好吧...的观点是它只会让代码更难被其他人阅读。

编辑:刚刚看到您更新的问题,您决定执行我上面提到的操作(似乎您在我回答的同时进行了更新)。说真的,我真的认为这是一个糟糕的设计模式,而且我只是将其作为一个假设的解决方案提到 - 我认为我最初并没有明确表示我实际上并不推荐它。

在我看来,你原来的 1 号版本是最好的方法,或者如果你必须使用某个功能,你的 2 号版本就可以了。

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.

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:

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文