当前位置:网站首页>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.
边栏推荐
猜你喜欢
小米手机短信定位服务激活失败
[Cloud native] Open source data analysis SPL easily copes with T+0
[swagger close] The production environment closes the swagger method
Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
What is GameFi?
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
【Elastic-Job源码分析】——作业监听器
2021美赛C题M奖思路
Using IIS10 to build an asp website in win11
阿里云中mysql数据库被攻击了,最终数据找回来了
随机推荐
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
MySQL compressed package installation, fool teaching
数字孪生将成为进入“元宇宙”一项重要的途径
【ubuntu20.04安装MySQL以及MySQL-workbench可视化工具】
Year-end summary - the years are quiet~
Detailed explanation of pointers in C language
Several forms of Attribute Changer
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
cocos2d-x-3.2 Physics
【云原生】微服务Nacos的简单介绍与使用
CMOS管原理,及其在推挽电路中的应用
[JVM Loading]---Class Loading Mechanism
sqlmap injection tutorial common commands
cocos2d-x 实现跨平台的目录遍历
【Elastic-Job】分布式调度任务概览篇
【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
cocos2d-x-3.2创建项目方法
A simple bash to powershell case
2021面经-拥抱变化
this指向问题