Created
February 27, 2024 04:01
-
-
Save karldreher/b4f6cefba65c9ef4130ea75423ef9698 to your computer and use it in GitHub Desktop.
Typescript Object members
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
/** | |
* Fruit class which details tastiness | |
* @property name - name of the fruit | |
* @property tastiness - how tasty the fruit is | |
* @property isCool - whether the fruit is cool or not | |
* @constructor - creates a new fruit with the given name, tastiness, and isCool properties | |
* @example | |
* ```ts | |
* new TastyFruit("Banana", 10, true) | |
* ``` | |
*/ | |
class TastyFruit{ | |
name: string; | |
tastiness: number; | |
isCool?: boolean; | |
base64name: string | |
constructor(name: string, tastiness: number, isCool?: boolean){ | |
this.name = name | |
this.tastiness = tastiness | |
this.isCool = isCool; | |
this.base64name = Buffer.from(name).toString('base64') | |
} | |
} | |
const banana = new TastyFruit("Banana", 7, false) | |
const cherry = new TastyFruit("Cherry", 10, true) | |
const grannySmithApple = new TastyFruit("Granny Smith Apple", 8, true) | |
const exampleObject = { | |
banana, | |
cherry, | |
grannySmithApple | |
} | |
// Construct a foreach loop using the exampleObject | |
console.log("members of exampleObject") | |
for (const o of Object.entries(exampleObject)) { | |
//o[0] is the representation of the key | |
console.log(o[0]) | |
} | |
//we can continue to access the members as properties | |
console.log("\nName of exampleObject.banana: ", exampleObject.banana.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment