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

后端 未结 3 1652
鱼传尺愫
鱼传尺愫 2021-01-20 23:05

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 confu

3条回答
  •  面向向阳花
    2021-01-20 23:39

    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

提交回复
热议问题