是否可以使用属性来删除匿名非IIFE JavaScript函数 [英] is it possible to delcare an anonymous non-IIFE JavaScript function with a property

查看:75
本文介绍了是否可以使用属性来删除匿名非IIFE JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经发现在将属性作为参数传递给其他函数之前,先将属性分配给函数很有用.

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.

类似的东西:

"(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"

意外的未处理"这是因为,令人困惑的是,JavaScript实际上是为funcOne和Two变量分配了值"true"和"false",并且在对条件if (argFunc.process)求值时它不会引发错误(无论我如何预期,"; strict mode").

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").

我已经使用IIFE对该功能进行了匿名工作,该IIFE将'anonymous'函数分配给一个变量,然后将该变量分配给该属性,然后将该变量从IIFE中返回,但是我希望可以使用JavaScript语法也许是这种更好的方法.

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.

就我的敏感性而言,我尝试过的dot属性分配没有任何意义.如果程序员想为匿名函数分配多个属性怎么办?

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?

我对var funcOne = function (arg1, arg2) { return arg1 + arg2; } = { process: true };抱有短暂的希望,但这引发了SyntaxError: Invalid left-hand side in assignment.

I had a short lived hope with var funcOne = function (arg1, arg2) { return arg1 + arg2; } = { process: true }; but that throws a SyntaxError: Invalid left-hand side in assignment.

推荐答案

要创建函数,您有两个选择:

To create a function, you have two options:

  • 函数声明

因此,不涉及任何表达式:

With this, no expressions are involved:

function funcOne(...) {
}

除了作为单独的独立语句外,无法处理funcOne.process = true之类的内容. (并不是说那是一件坏事-我实际上更喜欢这样的第二句话,它可能是最容易阅读的)

There is no way to tack on something like funcOne.process = true except as a separate, standalone statement. (Not that that's a bad thing - I'd actually prefer such a second statement, it's probably the easiest to read)

  • 函数表达式

有了这个,您就有一个函数表达式可以分配给变量名-但不能分配给变量名 ,因为=(赋值运算符)解析为已分配的,而不管=左侧的东西是什么.这就是为什么以下内容不起作用的原因:

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;

上面,分配的值是true,所以funcOne收到的值是true(没有对该函数的引用).

Above, the value that was assigned is true, so the value that funcOne receives is true (no reference to the function remains).

但是,您可以使用Object.assign(解析为分配给对象的 first 参数)来组合函数的声明和要分配给对象的其他属性.一个简单语句中的函数对象:

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:

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

这篇关于是否可以使用属性来删除匿名非IIFE JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆