当前位置:网站首页>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>
边栏推荐
- Graduated
- New listing of operation light 3.0 - a sincere work of self subversion across the times!
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- 培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知
- Random forest and poetry maker trained by AMR
- Analyze whether there is duplicate data in the list and repeat it several times
- Day30 JS notes BOM and DOM 2021.09.24
- 随机森林以及 AMR 训练出的诗词制造器
- Day39 prototype chain and page Fireworks Effect 2021.10.13
- day28 严格模式、字符串 js 2021.09.22
猜你喜欢

Industry analysis - quick intercom, building intercom

Fancy features and cheap prices! What is the true strength of Changan's new SUV?

Zero foundation self-study SQL course | if function

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Packaging and publishing application of jetpack compose desktop version

太阳能无线LED显示屏的特点

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022

day37 js笔记 运动函数 2021.10.11

day33 js笔记 事件(下)2021.09.28

Docker modifies the user name and password of MySQL
随机推荐
day37 js笔记 运动函数 2021.10.11
[sword finger offer] 49 Ugly number
Day29 JS notes 2021.09.23
Splicing strings in the string collection_ Stream based
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
Day28 strict mode, string JS 2021.09.22
Making and using of dynamic library (shared library)
随机森林以及 AMR 训练出的诗词制造器
Day31 JS notes DOM 2021.09.26
选择哪种编程语言,会吸引优秀的人才?
Unity屏幕截图功能
Wealth management for programmers
What is the main chain system?
Graduation season, some suggestions for you who are new to the society
js中的数组方法 2021.09.18
什么是DAPP系统发展与解析理解
ThreadLocal的简单理解
What is DAPP system development and analytical understanding
soapui的菜鸟教程
水果FL Studio/Cubase/Studio one音乐宿主软件对比