Search code examples
javascriptjqueryextend

How to extend jquery objects


I have tried using other examples on Stackoverflow to extend jquery objects but i have been unsuccessful.

    var z = {

        vars: {
            x: $('#c')
        }

    };

    var y = {

        v: $(this).val(a),
        h: $(this).html(a)

    };

    $.extend(y,z);

    alert(y.vars.h.x);

Solution

  • It looks like you have them extending correctly, you're just calling them wrong afterwards. I put the following together to help with debugging how it works.

    var z = {
        vars: {
            x: "var_Z.X"
        }
    };
    
    var y = {
        v: "var_Y.V",
        h: "var_Y.H"
    };
    
    $.extend( y, z );
    
    // Output
        ( y.vars.x ) // var_Z.X
        ( y.v ) // var_Y.V
        ( y.h ) // var_Y.H
    
        ( z.vars.x ) // var_Z.X
        ( z.v ) // undefined
        ( z.h ) // undefined
    

    http://jsfiddle.net/daCrosby/6YvHw/