Pass Object as an argument in Javascript

I have defined product as below

Now I am going to create an object using above constructor function
var product= new Product("Burger")

Now I want to change that object to represent drink name Coca Cola
Lets define method to change its properties

Here you are passing reference to the product object. So whatever changes inside the function will change the original object

Now we can call the changeProduct() method
>changeProduct(product)
Then you can call the getDiscount() method to see changes
>console.log("Discount is "+product.getDiscount())
>Discount is 0.6

Leave a Reply

Your email address will not be published. Required fields are marked *