当前位置:网站首页>function in js
function in js
2022-07-31 05:59:00 【messy me】
A function in JavaScript is also an object. Some functions (code) can be encapsulated in the function, and these functions (code) can be executed when needed.The function can save some code to be called when needed.
function function:- A function is also an object, some functions can be encapsulated in the function, and these functions can be executed when needed.- Functions can save some code to be called when needed. When using typeof to check a function object, it will return function.Creates a function object that can pass the code to be encapsulated as a string to the constructor.Code wrapped into a function does notExecute immediately, the code in the function will be executed when the function is called.Call function syntax: function object()Such as: var fun = new function(){console.log(1)};fun()The output result is 1;When a function is called, the code encapsulated in the function is executed in internal code order.
Create function: use function declaration, function expression to create:
function declaration syntax:function function name([parameter1, parameter2....parametern]){statement...}Use function expressions to create a functionvar function name = function([parameter 1, parameter 2....parameter n]){Statement....}
Formal parameters of the function:
Formal parameters of the function:One or more formal parameters (formal parameters) can be specified in () of a function.Used between multiple formal parameters, separated.Declaring a formal parameter is equivalent to declaring the corresponding variable inside the function.But don't assign a value to it.like:function sum(a,b){console.log(a+b);} is equivalent tofunction sum(a,b){var a; var b ;console.log(a+b);}When calling a function, you can specify arguments (actual parameters) in ().The actual parameter will be assigned to the corresponding formal parameter in the function.like:sum(1,2) where a and b are formal parameters.1 and 2 are the corresponding actual parameters.The calling function is a parser and does not check the type of the arguments.So you need to check some illegal parameters when using it.The parser also doesn't check the number of arguments when calling the function.Such as sum(1,2,h,o).During use, redundant arguments will not be assigned.If the number of actual parameters is less than the number of formal parameters, then the formal parameters without corresponding actual parameters will be undefined
Execute the function immediately:
function object()Immediately execute the function. After the function is defined, it is called immediately. This kind of function is called an immediate execution function.Immediate functions tend to be executed only once.like:(function(a,b){console.log("a="+a);console.log("b="+b);})(1,2);The output result is a=1, b=2.
Method:
The properties of an object can be any value, as well as a function.If a function is saved as a property of an object, then we call the function a method of the object, and calling the function is said to call the method of the object.It's just a name change.It is no different from other functions.
边栏推荐
猜你喜欢
随机推荐
mysql password modification method in Linux (pro-test available)
Fragmented NFT (Fractional NFT)
Build DVWA with phpstudy
flutter arr 依赖
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
【swagger关闭】生产环境关闭swagger方法
阿里一面,说说你知道消息中间件的应用场景有哪些?
【云原生】开源数据分析 SPL 轻松应对 T+0
MySQL高级SQL语句(二)
MySQL-如何分库分表?一看就懂
kotlin 插件更新到1.3.21
【uiautomation】微信好友列表获取(存储到txt中)
Access database query
自定dialog 布局没有居中解决方案
this指向问题
Android software security and reverse analysis reading notes
Using IIS10 to build an asp website in win11
Judgment of database in SQL injection
Xiaobai learns reptiles - introduction to reptiles