Wednesday, May 23, 2012

Javascript passing object as function parameter

I'm quite new to javascript, so maybe it's a silly error.
I created an object like the follwing:



function objA(){
this.prop1;
this.prop2;
this.prop3;
this.func1=function(){
alert('func1');
}
this.func2=function(){
alert('func2');
}
}


I now have a function where I want to pass the object:



var foo=new objA;

function test(foo){....}


The problem is that when I call test(), I get the functions in objA (objA.func1 and objA.func2) executed.
I would like just to get the properties value of objA.
I have to use another function and an array, fill the array with the properties of objA and then pass the array:



var arrayA={}

function fillArray(data){
arrayA.prop1=data.prop1;
arrayA.prop2=data.prop2;
arrayA.prop3=data.prop3;
}

function test(arrayA){....}


Is it the only way or I'm doing something wrong ?





No comments:

Post a Comment