Home > Article > Web Front-end > Briefly describe what a JavaScript object is
In JavaScript, there are only variables and functions. Variables hold data, and functions are special actions that are performed. JavaScript objects are just variables that hold variables and functions into a unit and encapsulate them as complete entities.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
What is a JavaScript object
In the world of JavaScript, we only have two things: variables and functions.
"Variables hold data, and functions are some special behaviors that will be executed." Variables can be int, string and float types, or they can be object types.
"JavaScript objects are just variables that hold variables and functions into a unit and encapsulate them as complete entities."
Understand JavaScript objects
In order to understand JavaScript objects, there is no need to create a large Asp.net MVC or Web Form project. We can do this with a simple HTML file
Step 1. Open Visual Studio. Click File>>New>>File. Select HTML
in the next screen Step 2. Create a JavaScript object as follows.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <script type="text/javascript"> var Customer = { Name: 'Anan Kumar', Address: 'USA' }; </script> </head> <body> </body> </html>
Step 3. Use "dot (.)" syntax to access properties within a JavaScript object as shown below.
alert(Customer.Name);
Step 4. Press Cntr S, provide some meaningful name and save the file.
Step 5. Open the HTML file with your favorite user and test the application.
Array JavaScript Object
Array - defined as a collection of elements of the same type. The good news is that you can even use JavaScript object arrays.
Step 1. The first step will be the same as above.
Step 2. The next step is to create an array of JavaScript objects as shown below and access it
var Customers = [ { Name: 'Anan Kumar', Address: 'USA' }, { Name: 'Rajav Kiran', Address: 'UK' }, { Name: 'Pappu Dabbawala', Address: 'USA' } ]; alert(Customers[0].Name);
Step 3. Save, execute and test the page.
Summary
The outer layer of the object is included with {}, and the inner layer is separated with,, while the object array uses [], which is similar to other languages, C and C The array mark here is also [].
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of Briefly describe what a JavaScript object is. For more information, please follow other related articles on the PHP Chinese website!