Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Forcing to Only Pass Value

    Goal: Have a function that return the value of the parameter without references. Most of the time, I want to return a copy of an array. (Not a function)

    Code:
    function valueOf(something){
    	return something; //The reference are kept...
    }
    Then I tried something like that but still not working...

    Code:
    function valueOf(something){
    	var whatToReturn;
    	
    	function getHandler(n) {
    		return n;
    	};
    
    	whatToReturn = getHandler(something);
    
    	return whatToReturn;
    }
    I don't know what to try next...

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function up(){
    	var mycars = new Array();
    mycars[0] = "Saab";
    mycars[1] = "Volvo";
    mycars[2] = "BMW";
    return mycars;
    }
    
    </script>
    </head>
    <body>
    <script type="text/javascript">
    bill = up();
    document.write(bill[0]+'<br>'+bill[1]+'<br>'+bill[2]+'<br>');
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Good luck. Won't happen with JavaScript (or Java or ...).

    Objects and arrays are always passed by reference.

    If you want to "clone" an object, then you have to (a) detect that it is an object and (b) write a cloning method.

    But even then you have to determine if you want a deep clone or a shallow clone.

    For example, say you have an array that looks like this:
    Code:
    var array1 = [ 3, "bob", [ 7, 12, 21 ] ];
    if you made a shallow clone of that array, then the *inner* array of [ 7, 12, 21 ] would still be just a reference in the clone.

    So to make a deep clone, you have to check each member of the object/array and, if it in turn is an object/array reference, clone it as well.

    http://www.lmgtfy.com/?q=javascript+deep+copy
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    IdkWhatsRc (06-09-2013)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by sunfighter View Post
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function up(){
    	var mycars = new Array();
    mycars[0] = "Saab";
    mycars[1] = "Volvo";
    mycars[2] = "BMW";
    return mycars;
    }
    
    </script>
    </head>
    <body>
    <script type="text/javascript">
    bill = up();
    document.write(bill[0]+'<br>'+bill[1]+'<br>'+bill[2]+'<br>');
    </script>
    </body>
    </html>
    I give up. How does that in any way demonstrate copying the value of an array to a new array?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I'm almost sure you were told once before that the way to clone an array is to use slice.

    So here's an example of a shallow copy of an array and why it *IS* just a shallow copy:
    Code:
    <script>
    function copyArrayValue( arr ) 
    {
        return arr.slice(0);
    }
    
    var ar1 = [ 7, "bob", [ 3, "ann" ], 9 ];
    var ar2 = copyArrayValue( ar1 );
    
    document.write( "ar1 before: " + ar1 + "<hr/>" );
    document.write( "ar2 before: " + ar2 + "<hr/>" );
    
    ar1[ 1 ] = "joe";
    ar1[ 2 ][ 1 ] = "mary";
    
    document.write( "ar1 after: " + ar1 + "<hr/>" );
    document.write( "ar2 after: " + ar2 + "<hr/>" );
    </script>
    Enough said?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I'm almost sure you were told once before that the way to clone an array is to use slice.

    So here's an example of a shallow copy of an array and why it *IS* just a shallow copy:
    Code:
    <script>
    function copyArrayValue( arr ) 
    {
        return arr.slice(0);
    }
    
    var ar1 = [ 7, "bob", [ 3, "ann" ], 9 ];
    var ar2 = copyArrayValue( ar1 );
    
    document.write( "ar1 before: " + ar1 + "<hr/>" );
    document.write( "ar2 before: " + ar2 + "<hr/>" );
    
    ar1[ 1 ] = "joe";
    ar1[ 2 ][ 1 ] = "mary";
    
    document.write( "ar1 after: " + ar1 + "<hr/>" );
    document.write( "ar2 after: " + ar2 + "<hr/>" );
    </script>
    Enough said?
    So I'd need to go inside each "dimension" of my array and do the .slice(0) trick?

  • #7
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay It's working now. I can go as deep as 7 dimensions with that.


    Code:
    function valueOf(arr){
    	
    	var copy = [];
    	for(d0 = 0 ; d0 < arr.length ; d0++){
    		copy.push([]);
    		
    		
    		if(typeof arr[d0] !== "object"){
    			copy[d0] = arr[d0];
    		} else {
    		
    			for(d1 = 0 ; d1 < arr[d0].length ; d1++){
    				copy[d0].push([]);
    			
    				
    				
    				if(typeof arr[d0][d1] !== "object"){
    					copy[d0][d1] = arr[d0][d1];
    				} else {
    				
    				
    				
    					for(d2 = 0 ; d2 < arr[d0][d1].length ; d2++){
    						copy[d0][d1].push([]);
    						
    						
    						
    						if(typeof arr[d0][d1][d2] !== "object"){
    							copy[d0][d1][d2] = arr[d0][d1][d2];
    						} else {
    						
    						
    						
    							for(d3 = 0 ; d3 < arr[d0][d1][d2].length ; d3++){
    								copy[d0][d1][d2].push([]);
    								
    								
    								
    								
    								if(typeof arr[d0][d1][d2][d3] !== "object"){
    									copy[d0][d1][d2][d3] = arr[d0][d1][d2][d3];
    								} else {
    								
    								
    									for(d4 = 0 ; d4 < arr[d0][d1][d2][d3].length ; d4++){
    									copy[d0][d1][d2][d3].push([]);
    									
    									
    									
    									
    									if(typeof arr[d0][d1][d2][d3][d4] !== "object"){
    										copy[d0][d1][d2][d3][d4] = arr[d0][d1][d2][d3][d4];
    									} else {
    								
    								
    								
    										for(d5 = 0 ; d5 < arr[d0][d1][d2][d3][d4].length ; d5++){
    										copy[d0][d1][d2][d3][d4].push([]);
    										
    										
    										
    										
    										if(typeof arr[d0][d1][d2][d3][d4][d5] !== "object"){
    											copy[d0][d1][d2][d3][d4][d5] = arr[d0][d1][d2][d3][d4][d5];
    										} else {
    								
    								
    											for(d6 = 0 ; d6 < arr[d0][d1][d2][d3][d4][d5].length ; d6++){
    											copy[d0][d1][d2][d3][d4][d5].push([]);
    											
    											
    											
    											
    											if(typeof arr[d0][d1][d2][d3][d4][d5][d6] !== "object"){
    												copy[d0][d1][d2][d3][d4][d5][d6] = arr[d0][d1][d2][d3][d4][d5][d6];
    											} else {
    								
    											
    								
    												for(d7 = 0 ; d7 < arr[d0][d1][d2][d3][d4][d5][d6].length ; d7++){
    												copy[d0][d1][d2][d3][d4][d5][d6].push([]);
    												
    												copy[d0][d1][d2][d3][d4][d5][d6][d7] = arr[d0][d1][d2][d3][d4][d5][d6][d7];
    								
    								
    							}
    						}
    						}}}}}}}}
    					}
    				}
    			}
    		}
    	
    	}
    	return copy;
    	
    	
    	
    	
    	
    	
    	
    }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    YOu have several mistakes in that code. It may work with some arrays, but it won't work with all of them. Plus it isn't making a distinction between arrays and objects.

    But the worst of all is that you didn't use recursion. Time to go read that google search I showed you.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Code:
    function valueOf(something){
    	return JSON.parse(JSON.stringify( something ));
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    Old Pedant (06-09-2013)

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    That is *EVIL* RndMe! LOL! It has to be about as efficient as a hummer's gas mileage. But who cares? You won't use it often, and it has to work like a charm. I love it!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Old Pedant View Post
    That is *EVIL* RndMe! LOL! It has to be about as efficient as a hummer's gas mileage. But who cares? You won't use it often, and it has to work like a charm. I love it!
    i don't know that it would be all that slow, JSON runs in C native routines, whereas user-land looping is about the slowest thing you can do, and the alternatives need a ton of looping.

    also note the key order is not guaranteed to be preserved, and that only JSON-capable primitives will be included, so any methods will be missing. You can define Function.prototype.toJSON and use the seldom-used revive parameter of JSON.parse to make new functions. Same goes for RegExp, Date, and DOM objects...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Oh, it's not perfect, but so what. It's more than adequate for 99% of all uses, I'm sure.

    TO tell you the truth, I can't even *REMEMBER* the last time I needed to clone an object. I know I have, but so so seldom that this is clearly the way to go. I really do like it, a lot.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •