简体   繁体   中英

How to change a variable when calling a function with parameters in javascript?

I have a function in my javascript file:

function hoverWidgetOn (param, value) {
    var element = $("*[data-label]"),
    config = {
        'display':'inline',
        'position':'absolute',
        'top':'6.5em',
        'padding' : '0.5em',
        'background-color':'#383838',
        'color':'white',
        'font-size' : '0.8em',
        'opacity' : '0.9',
        'param' : 'value'
    },
    label = $(this).attr("data-label"),
    d = document.createElement('span'),
    t = document.createTextNode(label);

    d.className = "labelShow";
    $(this).append(d);
    $('.labelShow').append(t).css(config);
}

What I want it to do is to add param and value to my variable config when calling function

element.on('mouseenter', hoverWidgetOn('background-color', 'red')) 

so the user of this application won't have to change my javascript file in order to change label's look while calling this function in other javascript file, but no matter how I try, this doesn't work... I would be glad if anyone can help me.

when you do this:

element.on('mouseenter', hoverWidgetOn('background-color', 'red')) 

hoverWidgetOn is called immediately.

You can do this:

element.on('mouseenter', function() { 
    hoverWidgetOn.call(this, 'background-color', 'red') 
}); 

Wrapping in an anonymous function will pass the function instead of executing it, and using call allows you to preserve the context of this .

You can modify your function to take param and value as arguments and return a function that will make an actual changes on mousenter event.

function hoverWidgetOn (param, value){
    return function() {
        var element = $("*[data-label]");
        var config = {'display':'inline',
            'position':'absolute',
            'top':'6.5em',
                'padding' : '0.5em',
            'background-color':'#383838',
            'color':'white',
            'font-size' : '0.8em',
            'opacity' : '0.9'};
        config[param] = value; //add your param

        var label = $(this).attr("data-label"),
        d = document.createElement('span');
        d.className = "labelShow";
        var t = document.createTextNode(label);
        $(this).append(d);
        $('.labelShow').append(t).css(config);
    };
}

You can only pass the function reference with the syntax you've got. To pass variables, you need to wrap your call in another function. You can use $.proxy to maintain the scope within that function. Try this:

element.on('mouseenter', function() {
    $.proxy(hoverWidgetOn('background-color', 'red')), this);
});

Also, to add a dynamic key/value to your config object you need to use array notation. In fact you have an odd mix of jQuery and JS in that function. Try this:

function hoverWidgetOn (param, value){
    var element = $("*[data-label]");
    var config = {
        'display': 'inline',
        'position': 'absolute',
        'top': '6.5em',
        'padding' : '0.5em',
        'background-color': '#383838',
        'color': 'white',
        'font-size': '0.8em',
        'opacity': '0.9'
    };
    config[param] = value;

    var label = $(this).attr("data-label"),
        $span = $('<span />', { 
            class = 'labelShow',
            text = label
        }).css(config);

Since you're using jQuery, you can pass custom object to the function, like this:

element.on('mouseenter', {param: 'background-color', value: 'red'}, hoverWidgetOn);

then to access this data:

function hoverWidgetOn(e) {
    var param = e.data.param;
    var value = e.data.value;
    (...)
}

EDIT :
My answer introduced very similiar solution (if not the same) to other answers so I proposed another approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM