Caspert
Caspert

Reputation: 4363

How to use two types of parameters within a function?

I call the following function to create a new css object:

set(
    dot, { 
        x: mX,  
        y: mY, 
        force3D: !0 
    }
);


set: function(el) {
    alert(el);

    var dot = $(el).css('-webkit-transform', 'translate3d(' + x + ', ' + y + ', ' + z + ')');
    return dot;
}

Now I was wondering how to get the first parameter of the function set, to be more precise: dot.

I already tried: alert(el); but this will give me an alert with [object Object]. I know you can access the object options by using el.x, but how to access the first parameter I doesn't know.

Upvotes: 0

Views: 53

Answers (2)

Taylor Daughtry
Taylor Daughtry

Reputation: 249

Is this what you're trying to do?

set = function(el) {
    console.log(el.x); // You can access the properties like this
    console.log(el.y);
    console.log(el.force3D);
    
    return dot;
};

// Build the params
var dot = {
    x: 'asdf',
    y: 'asdf',
    force3D: !0
};

// Call the set() method
set(dot);

Upvotes: 2

bishop
bishop

Reputation: 39374

I must be missing something, but if you're trying to access what you're sending, just name a formal parameter:

set = function(el, params) {
    var dot = $(el).css('-webkit-transform', 'translate3d(' + params.x + ', ' + params.y + ', ' + params.z + ')');
    return dot;
}

set(dot, { x:1.0, y:2.1, z:0.1 });

Or use arguments if you don't want formal named parameters.

Upvotes: 1

Related Questions