I'm from a c# c++ background and I'm confused about something that is happening.
Inside the scope of my function I am creating a new object before pushing it onto an 'array'. When I create the new object, it seems to be referencing the same object that was previously added to the array, even though I'm not assigning to the same variable. This is confusing because in c# this code would be creating a new object then adding the new object to the array. How do I get around this in javascript? Even using 'let', which I thought should stay in the scope I defined, does not seem to work
function:
addPurchaseOrderItem() {
this.purchaseOrderItems.push(new PurchaseOrder("","","","",1,100,""));
console.log(this.purchaseOrderItems);
// this.showNewPurchaseOrderItem = true;
}
another version that still does not work:
addPurchaseOrderItem() {
let temp = new PurchaseOrder("","","","",1,100,"");
this.purchaseOrderItems.push(temp);
console.log(this.purchaseOrderItems);
// this.showNewPurchaseOrderItem = true;
}
purchase order class:
export class PurchaseOrder {
public poNumber: string;
public siteLocation: string;
public lineItem: string;
public manufacture: string;
public partNum: string;
public qTY: number;
public acquisitionCost: number;
public itemDescription: string
constructor(
siteLocation: string,
lineItem: string,
manufacture: string,
partNum: string,
qTY: number,
acquisitionCost: number,
itemDescription: string
)
{
this.siteLocation = siteLocation;
this.lineItem = lineItem;
this.manufacture = manufacture;
this.partNum = partNum;
this.qTY = qTY;
this.acquisitionCost = acquisitionCost;
this.itemDescription = itemDescription
}
}
here is the live example (stackblitz): https://stackblitz.com/edit/angular-rvkjoa