Home > Article > Web Front-end > How to use methods in JavaScript
Methods are functions assigned to object properties. When a function is defined in an object's properties, it is called a method of the object rather than a function. This article will introduce you to methods in How to use methods in How to use methods in How to use methods in JavaScript. use.
Attributes are pre-set specific information (values) to which a name (attribute name) is added. In its properties, the function is specifically called a "method".
Let’s look at a specific example
In the following program, we create an object based on the traffic light blue, yellow, red and put it In a variable called traffic_light.
<!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <title>How to use methods in How to use methods in How to use methods in JavaScript</title> </head> <body> <script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop" } </script> </body> </html>
We added a property called current here. Contains the color of the traffic light in current.
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } </script>
The function that changes the color of the traffic light is defined as change_light. Then, change the value of this current by calling a method (function) called change_light.
We first define the function change_light
Continue to use function in the above program, let us temporarily define the function as change_light.
Let's consider the next signal calling change_light to determine the behavior of the next property to be called, depending on what is current at that time.
Use the switch statement to set change_light to four modes.
If the attribute contained in current is blue, the next attribute will be changed to yellow.
If the attribute contained in current is yellow, the next attribute will be changed to red.
If the attribute contained in current is red, the next attribute will be changed to blue.
The default is blue.
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } function change_light(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } </script>
Next, check the results by calling current in console.log
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } function change_light(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } change_light(); console.log(traffic_light.current); </script>
Because the default attribute is set to blue, the value of blue is output as go.
Use console.log to repeat the call three times...
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " " } function change_light(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } change_light(); console.log(traffic_light.current); change_light(); console.log(traffic_light.current); change_light(); console.log(traffic_light.current); change_light(); console.log(traffic_light.current); </script>
current changes from blue->yellow->red-> The output of the blue
value is go->slow down->stop->go
##Finally let us take a look See the use of the method that makeschange_light as traffic_light
What to do is to set the attribute name change_light after current: "" and use the following function to separate it (such as with ":" separate them). (At this point, the consecutive function name change_light is duplicated, so it can be deleted)<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " ", change_light:function(){ switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } } </script>Now, the teaffic_light object will have a method named change_light. We call it in consoe.log in the same way. Repeat this time four times. When calling each property in an object, you can call the property value by putting "." into the variable name followed by the property name. So when you want to call a method (property) in the object contained in the variable traffic_light, it becomes like this.
<script> var traffic_light = { blue: "go", yellow: "slow down", red: "stop", current: " ", change_light:function() { switch(traffic_light.current){ case traffic_light.blue: traffic_light.current = traffic_light.yellow; break; case traffic_light.yellow: traffic_light.current = traffic_light.red; break; case traffic_light.red: traffic_light.current = traffic_light.blue; break; default: traffic_light.current = traffic_light.blue; break } } } traffic_light.change_light(); console.log(traffic_light.current); traffic_light.change_light(); console.log(traffic_light.current); traffic_light.change_light(); console.log(traffic_light.current); traffic_light.change_light(); console.log(traffic_light.current); </script>The running results are as follows: The result does not change because the function change_light is just a method of the traffic_light object.
The above is the detailed content of How to use methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!