Ecmascript 710/8/2023 ![]() Next, we define an instance of the Song class, setting both fields via the constructor. We then output the fields to the console. The point is that song1.artist is not visible to the outside world, and outputs undefined. In Listing 1, we define a class, Song, using the class keyword. This class has two members, title and artist. The artist member is prefixed with a hash (#) symbol, so it is private. We allow for setting these fields in the constructor. Notice that the constructor must access this.#artist with the hash prefix again otherwise, it would overwrite the field with a public member. Let song1 = new Song("Only a Song", "Van Morrison") ![]() Previously, the standard approach when declaring a member field inside the class keyword was to introduce it in the constructor. The newest ECMAScript specification lets us define the member field inline as part of the class body. As shown in Listing 1, we can use a hashtag to denote a private field.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |