简体   繁体   English

是否可以使用属性来声明匿名非 IIFE JavaScript 函数

[英]is it possible to delcare an anonymous non-IIFE JavaScript function with a property

I have on one occasion found it useful to assign properties to functions before passing them as arguments to other functions.有一次我发现在将属性作为参数传递给其他函数之前为函数分配属性很有用。

That looked like this (sorry about any confusion between anonymous functions and variable-assigned function objects, I think they are not the same thing):看起来像这样(抱歉匿名函数和变量赋值函数对象之间的任何混淆,我认为它们不是一回事):

"(could strict mode have something to do with this?)"

var funcOne = function(arg1, arg2) { return arg1 + arg2; };
funcOne.process = true;
var funcTwo = function(arg1, arg2) { return arg1 + arg2; };
funcTwo.process = false;

var procFunc = function(argFunc) {
    if (argFunc.process) { 
        return argFunc(1,2);
    }
    return "not processed"
}

procFunc(funcOne); // 3
procFunc(funcTwo); // "not processed"

I would like to declare an anonymous function and simultaneously assign properties to it so when it is later called as part of a function array stack, conditional code can depend on the property.我想声明一个匿名函数并同时为其分配属性,以便稍后将其作为函数数组堆栈的一部分调用时,条件代码可以依赖于该属性。

Something like:就像是:

"(could strict mode have something to do with this?)"

var funcOne = function(arg1, arg2) { return arg1 + arg2; }.process = true;
var funcTwo = function(arg1, arg2) { return arg1 + arg2; }.process = false;

var procFunc = function(argFunc) {
    if (argFunc.process) { 
        return argFunc(1,2);
    }
    return "not processed"
}

procFunc(funcOne); // "not processed"
procFunc(funcTwo); // "not processed"

Where the unexpected "not processed" is because confusingly, JavaScript is actually assigning the value 'true' and 'false' to the funcOne and Two variables, and it does not throw an error when the conditional if (argFunc.process) is evaluated (regardless, as I suppose I expect, of "strict mode").意外的“未处理”是因为令人困惑,JavaScript 实际上是将值 'true' 和 'false' 分配给 funcOne 和 Two 变量,并且在评估条件if (argFunc.process)时它不会抛出错误(不管,正如我所期望的,“严格模式”)。

I have gotten this to work functionally anonymously using an IIFE that assigns the 'anonymous' function to a variable then assigns the property on that variable and returns that variable out of the IIFE, but I was hoping there was a JavaScript syntax for this or just a better way, maybe.我已经使用 IIFE 以匿名方式工作,该 IIFE 将“匿名”函数分配给变量,然后分配该变量的属性并从 IIFE 返回该变量,但我希望有一个 JavaScript 语法,或者只是更好的方法,也许。

To my sensibility, the dot property assignment I tried does not make sense.在我看来,我尝试的点属性分配没有意义。 What if programmer wanted to assign multiple properties to the anonymous function?如果程序员想为匿名函数分配多个属性怎么办?

I had a short lived hope with var funcOne = function (arg1, arg2) { return arg1 + arg2; } = { process: true };我有一个短暂的希望var funcOne = function (arg1, arg2) { return arg1 + arg2; } = { process: true }; var funcOne = function (arg1, arg2) { return arg1 + arg2; } = { process: true }; but that throws a SyntaxError: Invalid left-hand side in assignment .但这会引发SyntaxError: Invalid left-hand side in assignment

To create a function, you have two options:要创建函数,您有两个选择:

  • Function declaration函数声明

With this, no expressions are involved:有了这个,不涉及表达式:

function funcOne(...) {
}

There is no way to tack on something like funcOne.process = true except as a separate, standalone statement.除了作为单独的独立语句之外,没有办法添加类似funcOne.process = true东西。 (Not that that's a bad thing - I'd actually prefer such a second statement, it's probably the easiest to read) (并不是说这是一件坏事——我实际上更喜欢这样的第二个陈述,它可能是最容易阅读的)

  • Function expression函数表达式

With this, you have a function expression you can assign to a variable name - but you can't assign to the variable name and assign a property to the function at the same time with = , because = (the assignment operator) resolves to the value that was assigned , regardless of what sort of thing is on the left-hand side of the = .有了这个,您就有了一个可以分配给变量名称的函数表达式 - 但您不能同时分配给变量名称使用=为函数分配一个属性,因为= (赋值运算符)解析为分配的值,无论=的左侧是什么类型的东西。 This is why the following doesn't work:这就是以下不起作用的原因:

var funcOne = function x(arg1, arg2) { return arg1 + arg2; }.process = true;

Above, the value that was assigned is true , so the value that funcOne receives is true (no reference to the function remains).上面,分配的值是true ,所以funcOne接收到的值是true (没有对该函数的引用)。

But, you can use Object.assign , which resolves to the first parameter, the object that was assigned to, to combine the declaration of the function and the additional property you want to assign to the function object in a single mostly-concise statement:但是,您可以使用Object.assign ,它解析为第一个参数,分配给的对象,将函数的声明和要分配给函数对象的附加属性组合在一个简单的语句中:

 var funcOne = Object.assign( (arg1, arg2) => { return arg1 + arg2; }, { process: true } ); console.log(funcOne(3, 4));

functional composition is the right tract... here is a function for adding a prop to a different function.功能组合是正确的方法……这里是一个将道具添加到不同功能的功能。

var addProp = function(fun, propName, propVal){fun[propName] = propVal; return fun}


var funcOne = addProp(function(arg1,arg2){ return arg1 + arg2; }, "process",true);
var funcTwo = addProp(function(arg1,arg2){ return arg1 + arg2; }, "process",false);

the resulting code looks like that.生成的代码看起来像这样。 and behaves as expected并按预期行事

Another way of doing this would be with a helper function:另一种方法是使用辅助函数:

const addProp = (fn, value) => { fn.process = value; return fn; };

const myFunc = addProp((arg1, arg2) => arg1 + arg2, true);

console.log(myFunc.process); // => true
console.log(myFunc(1, 2));   // => 3

You could probably also do this with a decorator, although that's a proposal for future versions of ECMASCript and would require transpilation to work.您可能也可以使用装饰器来做到这一点,尽管这是对 ECMASCRipt 未来版本的建议,并且需要转译才能工作。

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

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