当前位置:网站首页>JS基础10
JS基础10
2022-06-28 10:51:00 【程序员社区】
函数补充
- 自调用函数
(function(){
console.log('自调用函数');
//结果自调用函数
})()
作用:形成一个独立作用域空间,空间中定义的变量都是私有的,不会对外面进行污染
2.arguments所有实参集合,是个伪数组
里面有个length属性确定实参个数
function fun(){
console.log(arguments);
//结果Arguments [10, callee: ƒ, Symbol(Symbol.iterator): ƒ]
}
fun(10)
- this关键字
var obj={
fu: function(){
console.log(this);
//对象内部指向obj
}
}
obj.fu() divEle=document.querySelector('div') divEle.οnclick=function(){
console.log(this);
//事件对象指向事件对象
}
function fun(){
console.log(this);
//全局定义函数指向window
}
fun() (function(){
console.log(this);
//自调用函数指向window
})() setTimeout(function(){
console.log(this);
},0)
//定时器函数指向window
- 改变this指向
- 函数.call(指向新对象)
- 函数.call(指向新对象,参数1,参数2)
- 函数.apply(指向新对象)
- 函数.apply(指向新对象,[参数1,参数2])
- var 新函数=函数.bind(指向新对象)新函数(参数)
<script> var obj={
name:'小明'
}
var obj2={
name:'小花'
}
var obj3={
name:'小华'
}
function test(a,b){
console.log(this);
console.log(a);
console.log(b);
}
test(1,2) //window 1 2 test.call(obj,1,2) //{
name: '小明'} 1 2 test.apply(obj2,[1,2]) //{
name: '小花'} 1 2 var test2=test.bind(obj3) test2(1,2) //{
name: '小华'} 1 2
</script>
- 箭头函数
定义:函数的简写形式
<script> function fun(){
}
//不能简写 const fun=function(){
}; //可以简写 const obj={
fun:function(){
}
//可以简写
}
</script>
箭头函数简写形式
<script> divEle=document.querySelector('div') divEle.οnclick=function(e){
console.log('测试简写');
}
//只有一个参数时可以简写小括号 divEle.οnclick=e=>{
console.log('测试简写');
}
//只有一行代码可以简写花括号,自动添加return返回
divEle.οnclick=e=>console.log('测试简写')
</script>
- 解构赋值
注:object用{},array用[]
let obj={
name:'jack',
age:18,
sex:'男'
}
let{
name,age}=obj let obj1={
arr:[1,2]}
let [a,b]=obj1.arr
应用:
<script>
//交换两个数
let a=10,
b=20;
[a,b]=[b,a]
console.log('a='+a+'b='+b);
//结果a=20b=10 //函数一般只返回一个值,用解构方法可以返回多个值 function fun(){
return [1,2,3]
}
var [a,b,c]=fun()
console.log(a b c);
//结果1,2,3 //传参时用解构可以不按顺序传入 function person({
name,age}){
console.log('我是'+name+'年龄'+age);
//结果我是jack年龄19
}
person({
age:19,name:'jack'}) //解构赋值可以快速拿出数组中的值 var arry=[10,20,30] var {
0:first,
2:last
}=arry
console.log(first);
//结果10
</script>
展开运算符…
<script>
//展开数组
let arry=[1,2,3]
console.log(...arry);
//结果1 2 3
//展开数组然后拼接
let arr=[1,2,3]
let arr2=[...arr,4]
console.log(arr2);
//[1,2,3,4] //展开对象然后拼接 let obj={
name:'jack',
age:19
}
let obj1={
...obj,
sex:'男'
}
console.log(obj1);
//结果{
name:'jack',age:19,sex:'男'}
</script>
边栏推荐
- Realize an air conditioner with compose to bring you cool in summer
- Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
- 广州海关支持保障食品、农产品和中药材等民生物资稳定供港
- 【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官
- Set up your own website (11)
- [Unity]内置渲染管线转URP
- 移动命令
- 满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
- How to use K-line diagram for technical analysis
- 数据库系列:有什么办法对数据库的业务表进行无缝升级
猜你喜欢
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
How does ETF position affect spot gold price?
Markdown -- basic usage syntax
数据库系列:有什么办法对数据库的业务表进行无缝升级
fastposter v2.8.4 发布 电商海报生成器
Day 6 script and animation system
How to use dataant to monitor Apache apisex
Installing MySQL database (CentOS) in Linux source code
移动命令
What is the best way to learn machine learning
随机推荐
MySQL(二)
Summary of characteristics of five wireless transmission protocols of Internet of things
爱可可AI前沿推介(6.28)
Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
Discard Tkinter! Simple configuration to quickly generate cool GUI!
工控安全之勒索病毒篇
mysql数据库概述以及安装过程
Realize an air conditioner with compose to bring you cool in summer
【agora】get 一个 agora_refptr 对象的用法示例
Move command
无线通信模块定点传输-点对多点的具体传输应用
MySQL cannot be opened. Flash back
Ribbon core source code analysis
Who knows if it is safe to open an account with CSC securities
How to use K-line diagram for technical analysis
关于FTP的协议了解
壓縮解壓
Katalon当中的使用循环for、while和if...else、break、continue
【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官