Object Oriented Programming
JavaScript is an object-oriented programming language (OOP). The OOP language gives us the ability to define our own objects and variable types.
Don't worry, we'll explain how to create our own objects in the Advanced JavaScript section. Now we're going to start learning about the built-in JavaScript objects and how to use them. From the beginning of the next section, we will explain these built-in JavaScript objects in turn.
Note: Objects are just a special kind of data. Objects have properties and methods.
. Simple Type
The simple types of JavaScript include numbers, string (String), Boolean (Boolean), NULL, and undefined values. All other values are objects.
2. The object
Objects in JavaScript are variable keying collections. In JavaScript, arrays, functions, and regular expressions are objects.
object is the container for the property. Each of these attributes has a name and a value. The name of the property can be any string, including an empty string. The property value can be any value other than the undefined value.
3. Methods of defining objects
(1) defined by literal quantity. For example:
var obj = {"Name": "Jim", "Age": 16};
(2) New keyword definition. For example:
The code is as follows |
Copy Code |
var obj = new Object (); Obj.name = "Jim"; Obj.age = 16;
|
4. Properties of objects
To get the property value of an object:
var obj = {"Name field": "Jim", "Age": 16};
var name =obj["Name field"]; Property string is a variable or is not a valid identifier when you use the
var age =obj.age;
Priority is given to use. However, when the property string is a constant and the property string is a valid identifier, you can use the
|| Operator can be used to populate default property values:
var status = Flight.status | | "Unkown";
An object's property value can be updated by an assignment statement:
Obj.age = 20;
Objects are passed by reference.
Properties in the object prototype (prototype) chain can also be accessed in objects.
The delete operator can be used to delete an object's properties.
In the following example, we use the length property of the string object to compute the number of characters in the string.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var txt= "Hello world!" document.write (Txt.length) </script> The above code output is: 12 |
Method
Method refers to the behavior (or functionality that can be done) that an object can perform.
In the following example, we use the toUpperCase () method of the string object to display the uppercase text.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var str= "Hello world!" document.write (Str.touppercase ()) </script> The above code output is: HELLO world! |
Common Object reference
JavaScript string (String) object instance
Returns the length of a string
Add a style to a string
Returns the position of the first occurrence of the specified text in a string-indexOf () method
Finds a specific character in a string, if found, returns the character-match () method
The character in the replacement string-replace ()
JavaScript Date (date) object instance
Use the date () method to return today's date and time
Use GetTime () to calculate how many years from 1970 to today
Use setFullYear () to set specific dates
Use toUTCString () to convert the date of the day (based on UTC) to a string
Use Getday () to show weeks, not just numbers
JavaScript Array (Array) instances
Create an array
Use the for...in declaration to traverse an element in an array
Merging two arrays-concat ()
Composition string with elements of an array-join ()
Type array-sort ()
Array of numbers-sort ()
JavaScript Boolean (Boolean) object instance
Check logical values
See also: Reference manual for JavaScript Boolean objects.
JavaScript Math (Arithmetic object) instance
Rounding numbers by using round ()
Use random () to return a random number from 0 to 1
Use Max () to return the larger number of two given numbers
Use min () to return the smaller number of two given numbers