Misters
Misters

Reputation: 1347

How can i send objects as parameter Javascript

I need to pass an array as parameter but i have a problem, i dont know how to explain it so here is the example:

I have this code:

var doc = document;

    var Class = {};

    Class.Validate = function(opc)
    {
        alert(opc.id);//
return Class;// when returns the object the alert trigger as expected showing "#name"
    };

Class.Validate({
    id: "#name",
})

But what im trying to do is this:

var Class = {};
    Class.Validate = function(opc)
    {
        alert(opc.name);//when the object is return show display "carlosmaria"
return Class;//

    };

Class.Validar({
    name: {field:"carlos",field:"maria"},
})

how can i archived that?

Upvotes: 0

Views: 71

Answers (2)

Gwennael Buchet
Gwennael Buchet

Reputation: 1307

alert(opc.name) should return something like {Object object} because it's an objet. The second point is that your object has twice "field" as property.

If you want to use an array, you should call this way:

Class.Validar({
    name: ["carlos", "maria"]
})

Then, you could loop over opc.name to concatenate a full name. Something like this:

Class.Validate = function(opc)
{
    var name = "";
    for (var i=0, len=opc.name.length; i<len; ++i) {
        name += opc.name[i];
    }
    alert(name);//when the object is return show display "carlosmaria"
    return Class;//
};

Upvotes: 2

Bergi
Bergi

Reputation: 665122

Consider using actual arrays (via array literals):

Class.Validate({
     name: ["carlos", "maria"]
});

Upvotes: 1

Related Questions