Last active
March 27, 2016 16:59
-
-
Save alexnm/8e560dc6c03dc6e58cb3 to your computer and use it in GitHub Desktop.
Example for constructor based object creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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