diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2022-08-06 12:06:09 -0700 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2022-08-07 21:36:29 -0700 |
commit | 5bbfb173bde45e03a7eaee8c7df6ae572e552d3f (patch) | |
tree | 7f1947e5cd291a26d8038dcf0a13cb1e8f470ab7 | |
parent | e5c5d830fe9268cc7bfddc0763521b0fadb8dc18 (diff) | |
download | gjs-5bbfb173bde45e03a7eaee8c7df6ae572e552d3f.tar.gz |
doc: Advise against modifying prototype of String
Format.format() was originally intended to be put on String.prototype, but
that is widely considered bad practice now.
-rw-r--r-- | doc/Modules.md | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/doc/Modules.md b/doc/Modules.md index 5b822b6f..38492271 100644 --- a/doc/Modules.md +++ b/doc/Modules.md @@ -109,16 +109,22 @@ let baz = Math.PI; // Using native template literals (Output: Pi to 2 decimal points: 3.14) `${foo} to ${bar*2} decimal points: ${baz.toFixed(bar*2)}` -// Applying format() to the string prototype const Format = imports.format; -String.prototype.format = Format.format; // Using format() (Output: Pi to 2 decimal points: 3.14) +Format.format.call("%s to %d decimal points: %.2f", foo, bar * 2, baz); + +// Applying format() to the string prototype (this is the old way, but +// is often considered bad practice now, especially in GNOME Shell +// extensions where other extensions might overwrite it. +// Consider not doing this!) +String.prototype.format = Format.format; "%s to %d decimal points: %.2f".format(foo, bar*2, baz); // Using format() with Gettext -_("%d:%d").format(11, 59); -Gettext.ngettext("I have %d apple", "I have %d apples", num).format(num); +Format.format.call(_("%d:%d"), 11, 59); +Format.format.call( + Gettext.ngettext("I have %d apple", "I have %d apples", num), num); ``` |