0

I'm trying to figure out how I can make a bit of code that takes an object as an argument and also returns and object. I understand how to return strings and arrays, but I don't quite understand how I could return objects or have them as arguments. A small example would be awesome!

(edit) Sorry if I was vague. I understand that the syntax is the same. I'm just having trouble understanding it practically. How (or why) would you want to return an object or have an object as an argument. I'm telling a story through code and I'm trying to figure out how (through console) to output the result of a returned object. I just don't understand how you'd have that practically.

Steee
  • 298
  • 2
  • 5
  • 13

3 Answers3

3
function helloObj(obj){
  var newobj = {};
  newobj.name = "Hello "+ obj.name;
  return newobj;
}

var user = {name:"Peter"};
console.log( helloObj(user).name  );
Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
  • Thank you. This helped me understand it more. Now it makes sense. All I was wanting was an example to help me put practical examples with lessons I'm learning. – Steee Jun 20 '13 at 05:24
  • @StevenP. great, but there is a lot of materials to learn , you know – Ivan Chernykh Jun 20 '13 at 05:34
2

Simple, just like normal variables

function example(yourObject) {
    var newObject={};
    newObject.title = yourObject.title;
    newObject.name="testing123";
    return newObject;
}
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • In this particular example there is no real need to return the object since you change the objects internals and whatever object is passes will stay changed after the `example` function is done. – HMR Jun 20 '13 at 06:08
2

Passing objects to functions can be tricky, JavaScript passes objects sort of by reference that means when you change the object in the function it'll be changed after the function is done:

function sortmyarray(arr){
  arr.sort();
}
function test(){
 var arr=[3,2,1];
 sortmyarray(arr);
 console.log(arr);//[1,2,3]
}
test();

This only works when you invoke a function that changes the object, assigning a new value would not work:

function sortmyarray(arr){
  arr=[1,2,3];
}
function test(){
 var arr=[3,2,1];
 sortmyarray(arr);
 console.log(arr);//[3,2,1]
}
test();

But you can re assign a value to arr in test by a value returned from sortmyarray

function sortmyarray(arr){
  return "hello world";
}
function test(){
 var arr=[3,2,1];
 arr=sortmyarray(arr);
 console.log(arr);//"hello world"
}
test();

As to why passing objects to a function; it is so you can re use code here is a sort function that is re used:

var myObj={
 toString:function(){
  var ret=[];i=0;
  for(i=0;i<this.arr.length;i++){
   ret.push(this.arr[i].col1);
   ret.push("\t");
   ret.push(this.arr[i].col2);
   ret.push("\n");
  }
  return ret.join("");
 },
 arr:[
  {col1:99,col2:1},
  {col1:1,col2:3},
  {col1:3,col2:1},
  {col1:1,col2:99}
 ]
}

function sortmyarray(arr, byColNames){
  var len=byColNames.length;
  arr.sort(function(a,b){
    var i=0;
    while(a[byColNames[i]]===b[byColNames[i]]
      && i<len){
      i++;
    }
    if(i===len){
     return 0;
    }
    return(a[byColNames[i]]>b[byColNames[i]])?1:-1;
  });
}
sortmyarray(myObj.arr,["col2"])
console.log(myObj.toString());
sortmyarray(myObj.arr,["col2","col1"]);
console.log(myObj.toString());
sortmyarray(myObj.arr,["col1","col2"]);
console.log(myObj.toString());
HMR
  • 37,593
  • 24
  • 91
  • 160
  • 2
    s/passes objects by reference/passes object references by value/. The difference between the two seems to confuse the hell out of people. – cHao Jun 20 '13 at 06:12
  • @cHao yes, you can only change a passed object by invoking functions of the object or assigning new values to it's properties. You can't change primitive types at all because they are immutable and you can't add properties to them. http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language (the answer with over 200 votes) – HMR Jun 20 '13 at 06:35