Home  >  Article  >  Web Front-end  >  How to create javascript dynamic function

How to create javascript dynamic function

coldplay.xixi
coldplay.xixiOriginal
2021-04-30 10:47:223478browse

The way to create a javascript dynamic function is to use the function object to define it. The basic format of the dynamic function [var variable name = new function ("parameter 1", "parameter 2", "parameter n", "execution statement ")].

How to create javascript dynamic function

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

How to create a javascript dynamic function:

Dynamic functions must be defined using function objects (function is an object in javascript, which is fixed and unchangeable. It stipulates that function The "f" of the object must be capitalized. When it is a function, it is a keyword used when defining the function: function ppp (x, y). When it is a function (when f is capitalized), it is an object in JavaScript)

Basic format for creating dynamic functions:

var 变量名 = new function(”参数1“,”参数2“,”参数n“,”执行语句”);

Use the new keyword (new is a keyword in javascript and is also fixed. We must use new to create this function object when defining a dynamic function)

First define a variable ar variable name. Here, the variable name is random, then use the new keyword to create a function object, and then assign this object to this arbitrary variable, which is the var variable. Name = new function

The parentheses after function first contain the parameters passed to the function, and then are separated by a comma (,) and then the code of the function to be performed by this function.

Example:

Code example:

<script languuage = "javascript">
var square = new function ("x","y","var sum ; sum = x+y;return sum;");
alert(square(2,3));
</script>

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to create javascript dynamic function. 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