Home  >  Article  >  Web Front-end  >  How to create objects in JavaScript

How to create objects in JavaScript

不言
不言Original
2018-12-15 14:13:512338browse

An object is a collection of attributes. A variable array is also an object. Objects can be used to manage data. An attribute is a specific name (value) prefixed with a name (attribute name). Let’s introduce it in detail in this article. Usage of How to create objects in How to create objects in How to create objects in JavaScript objects.

How to create objects in How to create objects in How to create objects in JavaScript

Let’s first look at how to create an object

Create a traffic_light (traffic light) variable and write a A program that displays messages in color.

The code is as follows

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <title>How to create objects in How to create objects in How to create objects in JavaScript</title>
  </head>
  <body>
    <script>
      var traffic_light = {
        blue: "go",
        yellow: "slow down",
        red: "stop"
      }
    </script>
 </body>
 </html>

Blue means "go", yellow means "slow down", and red means "stop". Objects with these three attributes represent the defined traffic_light Variables.

Let’s call these three defined attributes

The code is as follows

<script>
  var traffic_light = {
    blue: "go",
    yellow: "slow down",
    red: "stop"
  }  
  console.log(traffic_light.blue);
  </script>

The value of the attribute blue is called, and the running result is as follows

How to create objects in How to create objects in How to create objects in JavaScript

NextLet’s look at how to change the value of this property

Access the property name and assign another value to it.

Change the value of the attribute named blue from go to go fast. When you call it in console.log the code is as follows

<script>
  var traffic_light = {
    blue: "go",
    yellow: "slow down",
    red: "stop"
  }
  traffic_light.blue =  "go fast";
  console.log(traffic_light.blue);
</script>

The running effect is as follows

How to create objects in How to create objects in JavaScript

This is the basic method of using objects in How to create objects in How to create objects in How to create objects in JavaScript .

There is another wayYou can use new to create such an object.

var 对象名=new object()

Let’s take a closer look

Function can be an object

For example, when you create the hello function

The code is as follows

function hello() {
  alert("hello,PHP中文网!");
}
hello();

This is equivalent to the following code.

Here we use an object with an anonymous function named function().

You can execute it by assigning function() to a variable named hello and assigning parameters to the hello variable (like hello() in the last line).

var hello = function() {
  alert("hello,PHP中文网!");
}
hello();

Arrays can be objects

How to create objects in How to create objects in How to create objects in JavaScript arrays are also objects.

Wrap an array with an object and add a function. Therefore, we can say that it is not a pure array but an object that looks like an array.

How to create objects in How to create objects in How to create objects in JavaScript arrays have the following properties and methods. Other than that, it is the same as a normal object

There are the following types of array properties and methods.

Attribute length (get the length of the array)

Method slice()(Get the array elements)

Method reverse()(Reverse the order of the elements)

Method push() (add a new element at the end)

Use new to create an array object

var family = new Array();

We allocate an empty Array object to the variable family.

Here, I add "Tom", "Jerry", "Sucy" using var family = new Array("Tom", "Jerry", "Sucy"); then, via console.log(family ); to call.

The code is as follows

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>How to create objects in How to create objects in How to create objects in JavaScript</title>
  </head>
  <body>
    <script>
      var family= new Array("Tom","Jerry","Sucy");
      console.log(family);
    </script>
  </body>
</html>

The running effect is as follows

How to create objects in How to create objects in How to create objects in JavaScript

The above is the detailed content of How to create objects in JavaScript. 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