Meta tags:
Headings (most frequently used words):
javascript, the, for, web, getters, setters, objects, adding, logic, summary, chapter, article, toc, about, getting, started, basics, control, structures, data, types, built, in, operators, functions, classes, error, handling, regular, expressions, general, bom, dom,
Text of the page (most frequently used words):
the (99), name (50), dog (38), and (29), class (23), this (21), field (19), you (17), for (13), #javascript (12), methods (12), get (12), value (12), getters (12), #setters (11), introduction (11), set (9), new (9), can (8), but (8), doe (8), let (8), return (8), val (8), with (8), method (7), fields (7), that (6), john (6), operators (6), article (5), control (5), access (5), like (5), error (5), from (5), add (5), alert (5), have (5), setter (5), how (5), functions (5), operator (5), tutorial (4), has (4), inheritance (4), simple (4), which (4), are (4), called (4), more (4), them (4), first (4), then (4), part (4), example (4), arr (4), logic (4), instance (4), now (4), outside (4), into (3), your (3), consumer (3), still (3), validation (3), empty (3), errors (3), throwing (3), later (3), could (3), array (3), string (3), read (3), about (3), function (3), where (3), getter (3), inside (3), values (3), before (3), assign (3), prefixed (3), keyword (3), change (3), try (3), what (3), data (3), web (3), object (3), regular (3), expressions (3), catching (3), fully (2), language (2), not (2), here (2), previous (2), special (2), complete (2), they (2), allowing (2), way (2), out (2), summary (2), very (2), checking (2), user (2), submitted (2), throw (2), being (2), assigned (2), easily (2), last (2), looks (2), bit (2), complicated (2), split (2), enter (2), use (2), several (2), strings (2), show (2), something (2), useful (2), don (2), above (2), both (2), using (2), ability (2), returning (2), assigning (2), used (2), adding (2), see (2), parameter (2), want (2), need (2), basically (2), will (2), declare (2), property (2), notice (2), essentially (2), basic (2), happens (2), easy (2), number (2), classes (2), asp (2), net (2), dom (2), window (2), bom (2), search (2), types (2), conditional (2), catch (2), static (2), prototypes (2), objects (2), parameters (2), basics (2), loops (2), statement (2), toc (2), localization, suggest, correction, contact, com, 2007, 2026, been, translated, following, languages, preferred, list, help, translate, click, czech, next, allow, gain, appears, any, other, behind, scenes, creator, manipulate, some, process, prevent, etc, ensure, formatted, properly, simply, parts, make, sure, each, character, uppercase, rest, characters, lowercase, people, would, come, accomplish, articles, arrays, than, decided, instead, techniques, described, elsewhere, haven, yet, worry, cover, map, charat, touppercase, slice, tolowercase, join, trim, allowed, prompt, please, mind, created, version, powerful, feature, one, biggest, advantages, processing, especially, also, formatting, returned, whenever, unlike, takes, call, whatever, able, define, well, defined, makes, our, only, nothing, happen, because, declared, all, changed, indicates, learned, private, preventing, just, keywords, convey, their, meaning, start, looking, its, most, form, apply, concept, creators, take, back, layer, functionality, between, allows, often, referred, encapsulation, however, sometimes, anything, weird, there, really, create, saw, store, declaring, instances, stored, wpf, php5, jquery, html5, css3, webforms, mvc, location, history, script, tag, general, replace, capturing, groups, match, regex, flags, options, finally, handling, extension, accessors, properties, multiple, recursive, callback, anonymous, default, optional, chaining, nullish, coalescing, ternary, remainder, logical, modify, increment, decrement, arithmetic, comparison, assignment, json, math, date, built, type, conversion, null, undefined, numbers, booleans, while, switch, structures, truthy, falsy, variables, syntax, hello, world, tools, getting, started, libraries, webbrowser, chapter, table, contents,
Text of the page (random words):
getters setters the complete javascript tutorial javascript tutorial toc table of contents chapter article toc introduction fields methods getters setters getters setters adding logic summary inheritance static fields methods about javascript what is javascript javascript and the webbrowser javascript libraries getting started the tools you need hello world the basics basic javascript syntax variables truthy falsy control structures the if statement the switch statement loops for and while loops for of and for in data types introduction booleans numbers strings null undefined type conversion built in objects introduction number string array the basics array methods date math json operators introduction the assignment operator comparison operators arithmetic operators increment decrement operators modify and assign operators logical operators remainder operator conditional ternary operator nullish coalescing operator optional chaining operator functions introduction function parameters function parameters with default values anonymous functions callback functions recursive functions multiple return values objects introduction properties methods property accessors getters setters prototypes prototypes inheritance extension classes introduction fields methods getters setters inheritance static fields methods error handling introduction catching errors try catch catching throwing errors try catch finally error types conditional catching regular expressions introduction regex flags options search match with regular expressions using capturing groups search replace with regular expressions javascript for web general introduction window dom bom the script tag javascript for web the bom the window object the history object the location object javascript for web the dom introduction javascript asp net mvc asp net webforms c css3 html5 jquery php5 wpf classes getters setters in the previous article on class fields we saw how we could store data on a class by declaring a field and then access it on instances of the class very easily for instance we stored and used a name field like this class dog name dog doe let dog new dog dog name john dog alert dog name notice how i declare the name field inside the class and then later change the value from the instance of the dog class we create easy and simple however sometimes you don t want the access to your fields to be this easy for instance in the example above i as the consumer of the class could set the name to anything from a weird number to an empty string there s really no way to control what happens to this field but if we apply the concept of getters and setters we as the creators of the class can take this control back with getters and setters you essentially add a layer of functionality between the field and the consumer of the class which allows you to fully control what happens with the field this is often referred to as data encapsulation getters both getters and setters are essentially just functions but they are prefixed by the keywords get and set to convey their special meaning let s start by looking at the getters here s how it looks in its most basic form class dog name dog doe get name return this name first of all notice how i have changed the name of the field to name this indicates as we learned in the article on fields that the field is now private basically preventing access to it from outside of the class we then declare a method called name prefixed with the get keyword inside the method we return the value of the name field because the name method is declared with the get keyword we can access it like it s a property outside of the class class dog name dog doe get name return this name let dog new dog alert dog name we have now defined a getter but no setter which basically makes our field read only you will see this if you try to change the value of the field nothing will happen class dog name dog doe get name return this name let dog new dog dog name john dog alert dog name still dog doe if we want to be able to change the value of name outside of the class we now need to define a setter as well setters so let s add a setter to the class it s prefixed by the set keyword and unlike the get method it takes a parameter which is the value being assigned to the field you can call it whatever you like in my example i have called the parameter val class dog name dog doe get name return this name set name val this name val let dog new dog dog name john dog alert dog name john dog as you can see we now have the ability to assign a new value to the name field from outside the class whenever we do that the set name method is called where we can assign the new value to the field adding logic one of the biggest advantages of using getters and setters is the ability to add logic inside the methods allowing you to do processing of the values before returning and or assigning them this is especially useful for the setter where you can do validation of the value before assigning it to the field but it can also be used for the getter for instance to do formatting of the returned value before returning it with that in mind i have created a new version of the example above where we add logic to both the getter and setter to show you how powerful this feature is class dog name get name let arr this name split arr arr map function part return part charat 0 touppercase part slice 1 tolowercase return arr join set name val val val trim if val throw new error empty value not allowed this name val let dog new dog dog name prompt please enter dog name alert name of dog dog name this example is a bit more complicated than it has to be but i decided to show you something useful instead of something simple i use several techniques and methods described elsewhere in this tutorial but if you haven t read about them yet don t worry we ll cover it later on in the get method we ensure that the name is formatted properly it looks a bit complicated but we simply split the name into parts e g first name and last name and then we make sure that for each part the first character is in uppercase and that the rest of the characters are in lowercase so if people enter the name as john doe it would still come out as john doe we use several array and string methods to accomplish this read more about them in the articles on arrays and strings in the set method we do some very simple validation by checking if the user has submitted an empty value and if so we throw an error more on errors and the process of throwing them later on by throwing an error we prevent the name field from being assigned you could easily add more validation like checking if the user has submitted a first name and a last name etc summary the special get and set methods allow you to gain complete control of a class field for the consumer of the class it still appears as a simple field which they can access like any other field but behind the scenes the get and set methods are called allowing you the creator of the class to manipulate the value on the way in and out of the class methods previous inheritance next this article has been fully translated into the following languages czech is your preferred language not on the list click here to help us translate this article into your language javascript tutorial com 2007 2026 contact us suggest correction localization
|