Skip to content

Instantly share code, notes, and snippets.

@alexnm
Last active March 27, 2016 16:59
Show Gist options
  • Save alexnm/8e560dc6c03dc6e58cb3 to your computer and use it in GitHub Desktop.
Save alexnm/8e560dc6c03dc6e58cb3 to your computer and use it in GitHub Desktop.
Example for constructor based object creation
function Product( initialStock ) {
let stock = initialStock;
/* ... extra fields omitted for simplicity ... */
this.updateStock = function( newStock ) {
stock += newStock;
}
this.getStock = function( ) {
return stock;
}
};
const product = new Product( 4 );
console.log( product.stock ); // undefined!
console.log( product.getStock( ) ); // 4
product.updateStock( 3 );
console.log( product.getStock( ) ); // 7
const product2 = new Product( 0 );
console.log( product2.getStock( ) ); // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment