Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the function of JavaScript mean?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what the function of JavaScript means, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.

In JavaScript, a function is a piece of encapsulated code that can be used repeatedly (called) with a data type of "function". JavasScript functions can assign values, operate, own properties and methods, and even temporarily store values, construct instances, and so on; the JS function is executed when some code calls it.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, it is a piece of encapsulated code that can be used repeatedly (called) and belongs to "function" in the data type.

In JavaScript, a function can also be a value, an object, a class of data, or an expression, so the function can assign values, operate, have properties and methods (so the function is also an object), and even temporarily store values, construct instances, and so on.

The method of creating function

Functions can be created by three methods: function declaration, function expression and constructor.

In Javascript, a function definition function declaration or function expression starts with the keyword function, followed by these components:

1. Function name identifier. The function name is a necessary part of the function declaration statement. It is used like the name of a variable to which the newly defined function object is assigned. For function definition expressions, the name is optional: if it exists, the name exists only in the function body and refers to the function object itself.

2. A pair of parentheses (). It contains a list of 0 or more comma-separated identifiers, which are the parameter names of the function, just like local variables in the function body.

3. A pair of curly braces {}. It contains 0 or more JavaScript statements. These statements form the body of the function: once the function is called, these statements are executed.

First, function declaration

Define a function in javascript whose keyword function is at the beginning of the entire statement block, and the function code with a function name identifier is called a function declaration.

Example of function declaration code:

/ / example of function declaration function fDeclaration () {console.log ("this is an example of function declaration");} II. Function expression

Function expression:

Define a function in javascript that exists as a whole as an assignment statement of a variable or a statement executed by a call, and the block of the function statement is called a function expression.

/ / example of function expression var f = function f_Expression () {console.log ("this is an example of a function expression");} / / this is also a function expression, and the function is an anonymous function var fAnonymous = function () {console.log ("this is also an example of a function expression") } / / this is still a function expression, and the function is immediately executed (function () {console.log ("this is still an example of a function expression");} (); III. Constructor

A function called by the new keyword is called a constructor. Examples of common constructors:

Var f=new Function ("x", "y", "return x"; ")

This line of code creates a new function that is almost equivalent to the function defined by the following code:

Thank you for reading this article carefully. I hope the article "what is the function of JavaScript" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report