当前位置:网站首页>ES6中的函數進階學習
ES6中的函數進階學習
2022-07-07 10:06:00 【小白啥時候能進階成功】
1、函數定義方式
//1.自定義函數(命名函數)
function fn(){
}
//2.函數錶達式(匿名函數)
var fun = function(){
console.log("fun");
}
fun();
//3.利用new Function('參數1','參數2','函數體')
var f = new Function('a','b','console.log(a+b)');
f(1,2);函數也屬於對象。
2、函數的調用
//1.普通函數
function fn(){
console.log("this is a fun");
}
fn();//或采用fn.call();
//2.對象的方法
var o ={
sayHi :function(){
console.log("this is a object fun");
}
}
o.sayHi();
//3.構造函數
function Star(){
console.log('constructor fun');
}
let ldh = new Star();
//4.綁定事件函數
btn.onclick = function(){
console.log('btn click fun')
};//點擊了按鈕就可以調用這個函數
//5.定時器函數
setInterval(function(){console.log('setInterval fun')},1000);
//6.立即執行函數
(function(){
console.log('lijizhixing ');
})()3、this的指向問題
//1.普通函數-this指向window
function fn(){
console.log('1fn:'+this);
}
window.fn();
//2.對象的方法-this指向o這個對象
var o ={
sayHi :function(){
console.log(this);
}
}
o.sayHi();
//3.構造函數-this指向ldh這個實例對象
function Star(){
console.log(this);
}
let ldh = new Star();
//4.綁定事件函數-this指向btn這個按鈕
btn.onclick = function(){
console.log(this);
};
//5.定時器函數-this指向window
window.setTimeout(function(){console.log(this);},1000);
//6.立即執行函數-this指向window
(function(){
console.log(this);
})()總結:
| 調用方式 | this指向 |
| 普通函數 | window |
| 構造函數 | 實例對象(原型對象中的方法也指向實例對象) |
| 對象方法 | 該方法所屬對象 |
| 事件綁定 | 綁定事件對象 |
| 定時器 | window |
| 立即執行函數 | window |
4、改變函數內部this指向
常用的有call()、apply()、bind()三種方法
1、call方法
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
fn.call(o,1,2);//call的使用方法
//主要應用-繼承
function Father(uname,age)
{
this.uname = uname;
this.age = age;
}
function Son(uname,age)
{
Father.call(this,uname,age);
console.log('this name:'+this.uname);
}
let son1 = new Son('liming',18);
2、apply方法
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
fn.apply(o,[2,3]);//apply的用法,後面參數必須為數組
//主要應用案例-利用Math求最大值
var arr =[1,3,5,66,23,123];
let maxNum = Math.max.apply(Math,arr);
let minNum = Math.min.apply(Math,arr);
console.log("maxNum:"+maxNum+',minNum:'+minNum);3、bind方法(使用最多)
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
//使用方法一
let newFn = fn.bind(o);//bind的用法,他不調用函數,返回由指定this的原函數的拷貝
newFn(2,3);
//使用方法二
let newFn2 = fn.bind(o,2,3);//bind的用法,他不調用函數,返回由指定this的原函數的拷貝
newFn2();主要應用案例一
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
//使用
let newFn = fn.bind(o);//bind的用法,他不調用函數,返回由指定this的原函數的拷貝
newFn(2,3);
//主要應用
btn.onclick = function(){
this.disabled = true;//這裏的this指向按鈕
setTimeout(function(){
this.disabled = false;//這裏的this指向window
}.bind(this),3000);//bind綁定後,上面的this改為了按鈕
//等價於
// let fun =function(){
// this.disabled = false;//這裏的this指向window
// }.bind(this);
// setTimeout(fun,3000);//bind綁定後,上面的this改為了按鈕
}區別點:
- call和apply會調用函數,並且改變函數內部this指向。
- call和apply傳遞的參數不一樣,call傳遞參數arg1,arg2...,apply必須是數組形式[arg1,arg2...]
- bind不會調用函數,可以改變函數內部的this指向
主要應用場景:
- call經常做繼承
- apply經常和數組有關系。例如借助數學對象實現數組最大值最小值
- bind不調用函數,但是還想改變this指向。例如改變定時器內部的this指向。
边栏推荐
- PostgreSQL reports an error when creating a trigger,
- ORM--查询类型,关联查询
- Bean operation domain and life cycle
- Agile course training
- Application of C # XML
- Arcgis操作: 批量修改属性表
- Flex flexible layout
- Introduction to uboot
- flink. CDC sqlserver. 可以再次写入sqlserver中么 有连接器的 dem
- CDZSC_ 2022 winter vacation personal training match level 21 (1)
猜你喜欢

request对象对请求体,请求头参数的解析

CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)

ORM--查询类型,关联查询

【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)

小程序滑动、点击切换简洁UI

【ORM框架】

ORM--数据库增删改查操作逻辑

Win10安装VS2015

Agile course training
![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)
[original] what is the core of programmer team management?
随机推荐
2016 CCPC Hangzhou Onsite
有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
AI从感知走向智能认知
How to become a senior digital IC Design Engineer (1-6) Verilog coding Grammar: Classic Digital IC Design
字节跳动 Kitex 在森马电商场景的落地实践
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Switching value signal anti shake FB of PLC signal processing series
In addition to the objective reasons for overtime, what else is worth thinking about?
Gym - 102219j kitchen plates (violent or topological sequence)
HCIP 第一天 笔记整理
[untitled]
How to use Mongo shake to realize bidirectional synchronization of mongodb in shake database?
请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
Guys, how can mysql-cdc convert the upsert message to append only
CDZSC_ 2022 winter vacation personal training match level 21 (2)
基础篇:带你从头到尾玩转注解
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
【ORM框架】