No JavaScript overload concept

Check the fiddle or code below:

 function abc(s) {
     console.log('in abc(s)');
 }

 function abc(s, t) {
     console.log('in abc(s,t)');
 }

 abc('1');

The output of this question is always in abc(s,t)

Can someone please explain to me what is happening here and why?

+4
source share
3 answers

First, support for the overload method is not supported in JavaScript (see @ 6502 workaround ).

Secondly, to describe what you are experiencing in JavaScript, the last declared function (with the same name) is called, because the first has been overwritten, it refers to Raising JavaScript .

Try changing the order of function declarations and see the output again:

 function abc(s, t) {
     console.log('in abc(s,t)');
 }

function abc(s) {
     console.log('in abc(s)');
 }

 abc('1');
+4

Javascript .

, , arguments.

function foo(s, t) {
    if (arguments.length == 2) {
        ...
    } else {
        ...
    }
}

, , , undefined. , n- , arguments[i]. , , arguments Javascript, .

, , .

, function - , Python, . , , Python, Javascript:

console.log(square(12));
function square(x) { return x*x; }

. , ( script: , Javascript ).

, ( ), . , function if, ( IE, FF Chrome ), .

var square = function(x) { return x*x; }

" " , , ( if, ).

+6

javascript , .

, , , . , .

:

function abc(s, t) {
    // test to see if the t argument was passed
    if (t !== undefined) {
       console.log('was called as abc(s,t)');
    } else {
       console.log('was called as abc(s)');
    }
}

abc('1');       // outputs 'was called as abc(s)'
abc('1', '2');  // outputs 'was called as abc(s,t)'

, ( ).

, jQuery .css() .

.css( propertyName )
.css( propertyNames )
.css( propertyName, value )
.css( propertyName, function(index, value) )
.css( properties )

.css() , , , , , .

, , , 5 :

css: function(prop, value) {
    // first figure out if we only have one argument
    if (value === undefined) {
        if (typeof prop === "string") {
            // we have a simple request for a single css property by string name
            // of this form: .css( propertyName )
        } else if (Array.isArray(prop)) {
           // we have a request for an array of properties
           // of this form: .css( propertyNames )

        } else if (typeof prop === "object") {
           // property-value pairs of css to set
           // of this form: .css( properties )
        }
    } else {
        if (typeof value === "function") {
           // of this form: .css( propertyName, function(index, value) )
        } else {
           // of this form: .css( propertyName, value )
        }
    }
}

. , jQuery .hide() . - .hide( [duration ] [, complete ] ), , . , . :

 hide: function(duration, fn) {
     // default the duration to zero if not present
     duration = duration || 0;
     // default the completion function to a dummy function if not present
     fn = fn || function() {};

     // now the code can proceed knowing that there are valid arguments for both
     // duration and fn whether they were originally passed or not
 }

, , , , , , - . , javascript .add() :

s.add(key)
s.add(key1, key2, key3)
s.add([key1, key2, key3])
s.add(key1, [key8, key9], key2, [key4, key5])
s.add(otherSet)              // any other set object
s.add(arrayLikeObject)       // such as an HTMLCollection or nodeList

, , . , , , - . , , .

, GitHub , .

+1

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


All Articles