Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30271

duplicator an array

Newsgroups comp.lang.javascript
Date 2016-04-13 18:27 -0700
Message-ID <ea578169-9d58-4249-91b4-e0cbad5a6fd8@googlegroups.com> (permalink)
Subject duplicator an array
From JRough <janis.rough@gmail.com>

Show all headers | View raw


I have one question for you.
The problem is to create a duplicator function like below.  To make this one work:
[1,2,3,4,5].duplicator();
------------------------
Okay, first thing i did was just get it to work in a regular function.  I think this works because I saw the duplcation of res in Firebug.  
var arr =[1,2,3,4,5];
function duplicator(arr){
    return  arr.concat(arr.slice(0));
}
var res = duplicator(arr);
console.log(res.toString());
The only problem with this one is it doesn't output the string in the console.log.
Do you know why?

---------------------
Okay, supposing the above works then I thought I would make an anonymous function in the duplicator method.  This would make it work the way the problem was first outlined above.  The only problem is how do I capture the value of the two arrays?


I guess you would do something like Array.prototype.duplicator = (function (){
    return  this.concat(arr.slice(0));
})()

Back to comp.lang.javascript | Previous | NextNext in thread | Find similar | Unroll thread


Thread

duplicator an array JRough <janis.rough@gmail.com> - 2016-04-13 18:27 -0700
  Re: duplicator an array Aleksandro <aleksandro@gmx.com> - 2016-04-14 01:00 -0300
    Re: duplicator an array JRough <janis.rough@gmail.com> - 2016-04-14 09:53 -0700
    Re: duplicator an array "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-04-14 19:41 +0200
      Re: duplicator an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-14 22:09 +0200
        Re: duplicator an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-14 22:23 +0200
    Re: duplicator an array Stefan Weiss <krewecherl@gmail.com> - 2016-04-16 14:27 +0200
      Re: duplicator an array Joao Rodrigues <jr@none.com> - 2016-04-16 12:01 -0300
        Re: duplicator an array Joao Rodrigues <jr@none.com> - 2016-04-16 13:43 -0300
      Re: duplicator an array Aleksandro <aleksandro@gmx.com> - 2016-04-16 14:13 -0300
  Re: duplicator an array bharat.bandgar009@gmail.com - 2017-05-03 01:33 -0700

csiph-web