What is the purpose of declaring a function first? (variable declaration twice)

Here is the code I came across:

    var c = function() {},
        c = {
            autoSelectFirst: !1,
            appendTo: "body",
            serviceUrl: null,
            lookup: null,
            .. etc..
            lookupFilter: function(a, b, c) {
                return -1 !== a.value.toLowerCase().indexOf(c)
            },
            .. etc..
        };

Why is it cfirst declared as an empty function and then re-declared as an object? Isn't that just rewriting a function declaration? I believe this is the fundamental construction of JS.

+4
source share
2 answers

Based on this sample code from the original question:

var c = function() {},
    c = {
        autoSelectFirst: !1,
        appendTo: "body",
        serviceUrl: null,
        lookup: null,
        .. etc..
        lookupFilter: function(a, b, c) {
            return -1 !== a.value.toLowerCase().indexOf(c)
        },
        .. etc..
    };

The part c = function() {},in the code included in your question does not make sense. I like to do this:

var x = 1, x = 3;

It makes no sense to assign one value to a variable, and then immediately assign it another value.

, , ( ) :

    var c = {
        autoSelectFirst: !1,
        appendTo: "body",
        serviceUrl: null,
        lookup: null,
        .. etc..
        lookupFilter: function(a, b, c) {
            return -1 !== a.value.toLowerCase().indexOf(c)
        },
        .. etc..
    };

, , , .

FYI, Javascript " ". - Javascript.

+2

, , , , , .

, , - .

:

var c=function(){},c={autoSelectFirst:!1,appendTo:"body",serviceUrl:null,lookup:null,
onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},
formatResult:g.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:!1,
onSearchStart:c,onSearchComplete:c,onSearchError:c,
// -----------^------------------^---------------^

, , . , c onSearchStart:c,onSearchComplete:c,onSearchError:c,.

, c ? , , c , , onSearchStart, , .

.

, , . :

//   v---originally it called `noop`
var noop = function () { },
    that = this,
    defaults = {
        autoSelectFirst: false,
        appendTo: 'body',
        serviceUrl: null,
        lookup: null,
        onSelect: null,
        width: 'auto',
        minChars: 1,
        maxHeight: 300,
        deferRequestBy: 0,
        params: {},
        formatResult: YithAutocomplete.formatResult,
        delimiter: null,
        zIndex: 9999,
        type: 'GET',
        noCache: false,
        onSearchStart: noop,    // <---here it being used
        onSearchComplete: noop, // <---here it being used
        onSearchError: noop,    // <---here it being used

, , noop, no-operation, , . , noop .

, , minifier , , , noop, , . , IMO.

+7

All Articles