Often we need to represent an idea or concept in our programs — maybe a car engine, a computer file, a router, or a temperature reading. Representing these concepts directly in code comes in two parts: ES6 classes give us a convenient syntax for defining the state and behavior of objects that will represent our concepts.
This code gives us the strange output: So, our x starts as 5, and we display this. Then we call the changex function, which creates a local variable x, and initialises this to 10, and displays the local x value.
But outside the function, the global x is unchanged, as we see when we write it to the screen again. We can refer to the global x from the function as well. The global x is seen as a property of the window object.
Classes Classes can seem off-putting at first, but once you see the point of them, their use can be invaluable. We have already met objects. A computer object is a representation of a real object.
For an estate agent the object may be a house, including information about the number of rooms and the price. An estate agent may have a lot of houses available. These houses all have different characteristics, and as objects they all go through the same processes.
They are viewed, surveyed and bought, and so on. A full estate agent program would be difficult to demonstrate here, but we can introduce the use of classes. In this example, we have the house class. The house class produces house objects, all with object properties, such as number of rooms and price, and all having access to the same methods, such as sold and bought.
So a class can create objects with a group of properties and methods.
torosgazete.com THE WORLD'S LARGEST WEB DEVELOPER SITE.
History. Version ; License.