All values in JavaScript, with the exception of null
and undefined
, contain a set of auxiliary functions and values accessible "through the dot".
Such functions are called "methods", and values - "properties". Let's look at examples.
Example: str.length
, str.toUpperCase()
The string has a length
property that contains a length:
Strings also have a toUpperCase()
method that returns an uppercase string:
If a function is called through a point ( toUpperCase()
), this is called a “method call” if you simply read the value ( length
) - “property retrieval”.
Example: num.toFixed
Numbers have a num.toFixed(n)
method. It rounds the number num
to n
decimal places, if necessary, finishes with zeros to a given length and returns as a string (conveniently for formatted output):
The details of the toFixed
work are toFixed
in the Numbers chapter
The number method can also be addressed directly:
... But if the number is an integer, then there will be a problem:
The error will occur because JavaScript expects a decimal after a point.
This is a feature of JavaScript syntax. This is how it will work:
Pay attention, for the method call after its name there are brackets: hello.toUpperCase()
. Without brackets the method will not be called.
Let's see, for example, the result of a call to toUpperCase
without brackets:
This code displays the value of the toUpperCase
property, which is a function embedded in the language. Typically, the browser displays it something like this: "function toUpperCase() { [native code] }"
.
To get the result, this function must be called, and just for this in JavaScript brackets are required:
We will meet with lines and numbers in later chapters and learn more about the means to work with them.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone