0

So first off sorry for the bad question I didn't know how to ask it. What im trying to do is assign a variable to a class then use that variable to add duplicates of its specific class's attributes to an array. So

var apple = new Item("Apple", 5, 10); var items = []; items.push(new apple)

That doesn't work but i want to basically do that and am wondering how i would go about doing so.

Teight
  • 37
  • 3
  • 2
    you want to clone an object? is that it? perhaps https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript – Jaromanda X May 30 '18 at 23:30
  • None of your variables are classes. What attributes are you talking about? – Sebastian Simon May 30 '18 at 23:44
  • The simplest way to achieve it could be: 1. Implement an Item class method that allows you to get properties you are looking for. For example: Item.prototype.getProps = function() { return {name: this.name, qty: this.qty, price: this.price }; } 2. Then just call this method on an instance when saving props to an array: items.push(apple.getProps()); Btw, watch out for the wrong syntax: `new` can only be used with a class constructor, and you have an instance there. – Alex May 30 '18 at 23:52
  • A `new` instance creates a `new` Object. Why not just put the `new` instances in your Array? – StackSlave May 31 '18 at 00:10

2 Answers2

0

You can write a method that generates a clone depending on how specific and dynamic you need it to be. But aside from that, I'm not too familiar with anything that's native to Javascript.

class Person
{
  constructor(name, age, weight)
  {
    this.name = name;
    this.age = age;
    this.weight = weight;
  }

  Clone()
  {
    let myCopy = new Person(this.age, this.weight, this.weight);
    return myCopy;
  }
}

let me = new Person('Eddie', 29, 345);
let myTwin = me.Clone();
console.log(me, myTwin);

A complete and deep clone would be a little bit of overhead. It'd more than likely have to be able to identify all data types down to primitives and react for each one. Cloning an array might involve cloning the actual array and every value inside of it.

Every value inside of it might as well be a container that needs to follow the same process.

Eddie D
  • 1,120
  • 7
  • 16
0

I created this recursive copy function that may do what you want:

function copy(mixed){
  var o, n;
  if(typeof mixed === 'object' && mixed !== null){
    if(mixed instanceof Array){
      o = [];
      for(var i=0,l=mixed.length; i<l; i++){
        n = mixed[i];
        if(typeof n === 'object'){
          n = copy(n);
        }
        o.push(n);
      }
    }
    else{
      o = {};
      for(var i in mixed){
        if(mixed.hasOwnProperty(i)){
          n = mixed[i];
          if(typeof n === 'object'){
            n = copy(n);
          }
          o[i] = n;
        }
      }
    }
  }
  else{
    o = mixed;
  }
  return o;
}
// array test
var testArray = [0, 'test', 2, {prop:'val', num:5}]
var newTestArray = copy(testArray);
testArray[3] = {prop:'another val', num:7, ary:[0, 1, 7]};
console.log(testArray);
console.log(newTestArray);

// object test
function Item(itemName, width, height){
  this.name = itemName; this.width = width; this.height = height;
}
var testObj = new Item('Apple', 5, 10);
var newTestObj = copy(testObj);
testObj.width = 30;
console.log(testObj);
console.log(newTestObj);

In your case, however, you probably want to just do this:

function Item(itemName, width, height){
  this.name = itemName; this.width = width; this.height = height;
}
var items = [new Item('Apple', 5, 10), new Item('Orange', 7, 25), new Item('Peach', 12, 30)];
console.log(items);
StackSlave
  • 10,613
  • 2
  • 18
  • 35