Javascript handling calling functions named Args and Normal

If I have a function:

function(foo, bar, baz);

And I want to resolve both named arguments and normal function calls, what is the best way to handle this? In php, you can extract variables to a local namespace, but as far as I know, the only way to handle this in javascript is to process both scripts separately. I gave an example code below:

function(foo, bar, baz)
{
    if(typeof(foo) == 'object') // Named args
    {
        alert(foo.foo);
        alert(foo.bar);
        alert(foo.baz);
    }
    else
    {
        alert(foo);
        alert(bar);
        alert(baz);
    }
}

myFunc('a', 'b', 'c');
myFunc({ foo: 'a', bar: 'b', baz: 'c' });

Any javascript gurus who can teach me how to javascriptFu?

+3
source share
3 answers

Since you cannot access the local area dynamically (without evil eval), you should consider the following approach:

var myFunc = function (foo, bar, baz) {
    if (typeof(foo) === 'object') {
        bar = foo.bar;
        baz = foo.baz;
        foo = foo.foo; // note: foo gets assigned after all other variables
    }

    alert(foo);
    alert(bar);
    alert(baz);
};

args . .

+3

:

var myFunc = (function (foo, bar, baz) {
                   // does whatever it is supposed to do
               }).
    withNamedArguments({foo:"default for foo", bar:"bar", baz:23 });

myFunc({foo:1}); // calls function(1, "bar", 23)
myFunc({});  // calls function("default for foo", "bar", 23);
myFunc({corrupt:1}); // calls function({corrupt:1})
myFunc([2,4], 1);  //calls function([2,4], 1)

Array.prototype.slice =
    Array.prototype.slice.withNamedArguments({start:0, length:undefined});

[1,2,3].slice({length:2}) //returns [1,2]
[1,2,3].slice(1,2) //returns [2,3]

... , parseInt()

parseInt = parseInt.withNamedArguments({str:undefined, base:10});
parseInt({str:"010"}); //returns 10

Function:

Function.prototype.withNamedArguments = function( argumentList ) {
    var actualFunction = this;
    var idx=[];
    var ids=[];
    var argCount=0;
    // construct index and ids lookup table
    for ( var identifier in argumentList ){
        idx[identifier] = argCount;
        ids[argCount] = identifier;

        argCount++;
    }

    return function( onlyArg ) {
        var actualParams=[];
        var namedArguments=false;

        // determine call mode
        if ( arguments.length == 1 && onlyArg instanceof Object ) {
            namedArguments = true;
            // assume named arguments at the moment
            onlyArg = arguments[0];
            for ( name in onlyArg )
                if (name in argumentList ) {
                    actualParams[idx[name]] = onlyArg[name];
                } else {
                    namedArguments = false;
                    break;
                }
        }
        if ( namedArguments ) {
            // fill in default values
            for ( var i = 0; i < argCount; i++ ) {
                if ( actualParams[i] === undefined )
                    actualParams[i] = argumentList[ids[i]];
            }
        } else 
            actualParams = arguments;

        return actualFunction.apply( this, actualParams );
    };
};
+1

, , , typeof .

, , DTO ( , ). , , , .

// translate to named args - messy up front, cleaner to work with
function(foo, bar, baz) 
{
    // Opt 1: default to named arg, else try foo DTO
    bar = (typeof(bar) != 'undefined' ? bar : foo.bar);

    // Opt 2: default to named arg, else check if property of foo, else hard default (to null)
    baz = (typeof(baz) != 'undefined' ? baz : typeof(foo.baz) != 'undefined' ? foo.baz : null);

    // the first argument is always a problem to identify in itself
    foo = (foo != null ? typeof(foo.foo) != 'undefined' ? foo.foo : foo : null);
}

// translate to object - cleaner up front, messier to work with
function(foo, bar, baz) 
{
    var input = (typeof(foo.foo) != 'undefined' ? foo : { 'foo' : foo, 'bar' : bar, 'baz' : baz });
}

The first arg (foo here) is always a problem, because you expect it to be in one of two complex states (where the other args are always one complex state or undefined), and you cannot handle it, I dealt with all other arguments, because obviously, after you change it, it cannot use it to initialize anything else.

0
source

Source: https://habr.com/ru/post/1774474/


All Articles