Home  >  Article  >  Web Front-end  >  Briefly describe what a JavaScript object is

Briefly describe what a JavaScript object is

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-15 14:30:592444browse

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.

Briefly describe what a JavaScript object is

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: &#39;Anan Kumar&#39;, Address: &#39;USA&#39;
};
</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: &#39;Anan Kumar&#39;, Address: &#39;USA&#39;
},
{
Name: &#39;Rajav Kiran&#39;, Address: &#39;UK&#39;
},
{
Name: &#39;Pappu Dabbawala&#39;, Address: &#39;USA&#39;
}
];
 

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn