当前位置:网站首页>JS foundation 10
JS foundation 10
2022-06-28 11:43:00 【Programmer community】
Function addition
- Self calling function
(function(){
console.log(' Self calling function ');
// The result is from calling the function
})()
effect : Form an independent scope space , Variables defined in space are private , It won't pollute the outside
2.arguments All argument sets , It's a pseudo array
There's a length Property determines the number of arguments
function fun(){
console.log(arguments);
// result Arguments [10, callee: ƒ, Symbol(Symbol.iterator): ƒ]
}
fun(10)
- this keyword
var obj={
fu: function(){
console.log(this);
// The object points to obj
}
}
obj.fu() divEle=document.querySelector('div') divEle.οnclick=function(){
console.log(this);
// The event object points to the event object
}
function fun(){
console.log(this);
// The global definition function points to window
}
fun() (function(){
console.log(this);
// The self calling function points to window
})() setTimeout(function(){
console.log(this);
},0)
// The timer function points to window
- change this Point to
- function .call( Point to a new object )
- function .call( Point to a new object , Parameters 1, Parameters 2)
- function .apply( Point to a new object )
- function .apply( Point to a new object ,[ Parameters 1, Parameters 2])
- var New functions = function .bind( Point to a new object ) New functions ( Parameters )
<script> var obj={
name:' Xiao Ming '
}
var obj2={
name:' floret '
}
var obj3={
name:' Xiaohua '
}
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: ' Xiao Ming '} 1 2 test.apply(obj2,[1,2]) //{
name: ' floret '} 1 2 var test2=test.bind(obj3) test2(1,2) //{
name: ' Xiaohua '} 1 2
</script>
- Arrow function
Definition : The short form of a function
<script> function fun(){
}
// It can't be abbreviated const fun=function(){
}; // It can be abbreviated const obj={
fun:function(){
}
// It can be abbreviated
}
</script>
Abbreviated form of arrow function
<script> divEle=document.querySelector('div') divEle.οnclick=function(e){
console.log(' Test abbreviation ');
}
// Parentheses can be abbreviated when there is only one parameter divEle.οnclick=e=>{
console.log(' Test abbreviation ');
}
// Only one line of code can abbreviate curly braces , Automatic addition return return
divEle.οnclick=e=>console.log(' Test abbreviation ')
</script>
- Deconstruct assignment
notes :object use {},array use []
let obj={
name:'jack',
age:18,
sex:' male '
}
let{
name,age}=obj let obj1={
arr:[1,2]}
let [a,b]=obj1.arr
application :
<script>
// Exchange two numbers
let a=10,
b=20;
[a,b]=[b,a]
console.log('a='+a+'b='+b);
// result a=20b=10 // Functions generally return only one value , Multiple values can be returned by deconstruction method function fun(){
return [1,2,3]
}
var [a,b,c]=fun()
console.log(a b c);
// result 1,2,3 // When passing parameters, deconstruction can be used to pass in... Out of order function person({
name,age}){
console.log(' I am a '+name+' Age '+age);
// As a result, I was jack Age 19
}
person({
age:19,name:'jack'}) // Deconstruction assignment can quickly take out the values in the array var arry=[10,20,30] var {
0:first,
2:last
}=arry
console.log(first);
// result 10
</script>
Expand operator …
<script>
// Expand array
let arry=[1,2,3]
console.log(...arry);
// result 1 2 3
// Expand the array and splice
let arr=[1,2,3]
let arr2=[...arr,4]
console.log(arr2);
//[1,2,3,4] // Expand the object and splice let obj={
name:'jack',
age:19
}
let obj1={
...obj,
sex:' male '
}
console.log(obj1);
// result {
name:'jack',age:19,sex:' male '}
</script>
边栏推荐
- 近况
- Simple understanding of ThreadLocal
- 董宇辉,新东方以及凤凰卫视
- 什么是DAPP系统发展与解析理解
- 科研丨Web of Science检索技巧
- day23 js笔记 2021.09.14
- 智联招聘基于 Nebula Graph 的推荐实践分享
- Convert the file URL in the browser to a file stream
- setInterval、setTimeout和requestAnimationFrame
- Redis6 1: what problems can be solved by the introduction of NoSQL and redis?
猜你喜欢

Redis6 一:Nosql引入、Redis可以解决什么问题?

Day37 JS note motion function 2021.10.11

赛尔号抽奖模拟求期望

day39 原型链及页面烟花效果 2021.10.13

Characteristics of solar wireless LED display

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Day36 JS notes ecma6 syntax 2021.10.09

Web page tips this site is unsafe solution

2022中国信通院首届业务与应用安全发展论坛成功召开!

Array method in JS 2021.09.18
随机推荐
day23 js笔记 2021.09.14
Apache2配置对目录拒绝访问,但是可以访问里面文件的设置
Recommended practice sharing of Zhilian recruitment based on Nebula graph
一套十万级TPS的IM综合消息系统的架构实践与思考
day39 原型鏈及頁面烟花效果 2021.10.13
什么是主链系统?
Zero foundation self-study SQL course | if function
On the output representation of bidirectional LSTM in pytoch
[sword finger offer] 49 Ugly number
recent developments
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
关于Pytorch中双向LSTM的输出表示问题
工作组环境下的内网渗透:一些基础打法
day29 js笔记 2021.09.23
Create ECS using API shortcut
setInterval、setTimeout和requestAnimationFrame
合约量化交易系统开发 | 合约量化APP开发(现成案例)
Lihongyi, machine learning 7 Conclusion
SoapUI rookie tutorial
董宇辉,新东方以及凤凰卫视