Is it possible to pass data this way

Hello,
Is it possible and how to pass data this way?


var client = [];
var arrays = [1, 2, 3, 4, 5];

function passArray() {
	getArray(arrays);
}

function getArray(a) {
	this.a = a;
	client.push(a);
	if (client.length < 5) {
		client.retrieveArray(a);
	}
}

function retrieveArray(i) {
  	this.i = i;
	console.log(i);
}

var button = document.getElementById('button');
button.addEventListener('click', passArray, false);

As I understand this, you’re trying to make a deep copy of arrays into client, on button click. If so, for primitive types, you could do this:


var client = [];
var arrays = [1, 2, 3, 4, 5];

function deepCopyPrimitiveValuesInArray() {
	client = arrays.slice(0);
	// client = arrays.slice(); // this works too
}

var button = document.getElementById('button');
button.addEventListener('click', deepCopyPrimitiveValuesInArray, false);

If your arrays array contains complex types, things get a little complicated… For simplicity I’d recommend:

Things to be aware of:

Lo-Dash’s cloneDeep

Functions and DOM nodes are not cloned. The enumerable properties of arguments objects and objects created by constructors other than Object are cloned to plain Object objects.

jQuery’s [deep] extend

On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not.

Thanks, it works, and thanks for additional info.