当前位置:网站首页>call方法和apply方法
call方法和apply方法
2022-06-12 08:09:00 【李大大大哥】
1.定义:
两个方法都是函数对象的方法,需要通过函数对象来调用
当对函数call()和apply()都调用函数执行
调用call()和apply()可以将一个对象指定未第一个参在此时这个对象将会成为函数执行时的this
call()方法可以将实参在对象之后依次传递
apply()方法需要将实参封装到一个数组中传递
this的情况:
1.以函数形式调用时,this永远都是window
2.以方法的形式调用时this是调用方法的对象
3.以构造函数的形式调用时其是新创建的那个对象
4.使用call和apply调用时this时指定的那个对象
2. 例子
var obj={
name:"张三",
age:20
};
function method(){
console.log(this);//window
}
method();
this指向的是window,那么怎么让他指向当前对象obj呢?
方法1:使用call
var obj={
name:"张三",
age:20
};
function method(a,b,c){
console.log(this,a,b,c);//{name: "张三", age: 20} 1 2 3
}
method.call(obj,1,2,3);
方法2:使用apply
var obj={
name:“张三”,
age:20
};
function method(a,b,c){
console.log(this,a,b,c);//{name: “张三”, age: 20} 1 2 3
}
method.apply(obj,[1,2,3]);
边栏推荐
- Ceres optimizer usage (self use)
- Fundamentals of Mathematics - Taylor Theorem
- (P13)final关键字的使用
- Conda创建虚拟环境报错,问题解决
- Vision Transformer | Arxiv 2205 - TRT-ViT 面向 TensorRT 的 Vision Transformer
- MES系统质量追溯功能,到底在追什么?
- DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ... Reading notes
- (P25-P26)基于非范围的for循环、基于范围的for循环需要注意的3个细节
- 千万别把MES只当做工具,不然会错过最重要的东西
- 工厂的生产效益,MES系统如何提供?
猜你喜欢

Model Trick | CVPR 2022 Oral - Stochastic Backpropagation A Memory Efficient Strategy

从AC5到AC6转型之路(1)——补救和准备

Vision Transformer | CVPR 2022 - Vision Transformer with Deformable Attention

超全MES系统知识普及,必读此文

Talk about the four basic concepts of database system

Vins technical route and code explanation

如何理解APS系统的生产排程?

Transformation from AC5 to AC6 (1) - remedy and preparation

Detailed explanation of Google open source sfmlearner paper combining in-depth learning slam -unsupervised learning of depth and ego motion from video

记录谷粒商城踩坑(一)
随机推荐
DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ... Reading notes
DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ...阅读笔记
2.2 链表---设计链表(Leetcode 707)
S-msckf/msckf-vio technical route and code details online blog summary
Vision Transformer | Arxiv 2205 - TRT-ViT 面向 TensorRT 的 Vision Transformer
Cmake can't find the solution of sophus
(p36-p39) right value and right value reference, role and use of right value reference, derivation of undetermined reference type, and transfer of right value reference
Servlet advanced
TMUX common commands
Detailed explanation of Google open source sfmlearner paper combining in-depth learning slam -unsupervised learning of depth and ego motion from video
Fundamentals of Mathematics - Taylor Theorem
Clarify the division of IPv4 addresses
MES系统是什么?MES系统的操作流程是怎样?
JSP technology
Procedure execution failed 1449 exception
(P25-P26)基于非范围的for循环、基于范围的for循环需要注意的3个细节
What is an extension method- What are Extension Methods?
模型压缩 | TIP 2022 - 蒸馏位置自适应:Spot-adaptive Knowledge Distillation
Compiling principle on computer -- functional drawing language (I)
Vscode 调试TS