当前位置:网站首页>This point - super classic interview questions
This point - super classic interview questions
2022-07-26 20:22:00 【Coexistence source of beauty and strength】
this Binding rules of
Bind one : The default binding ;
Independent function call
Independent function call can be understood as that the function is not bound to an object for call
Calls to independent functions Point to global object window
Binding two : Implicit binding ;
Called through an object , Functional this The binding of is the location of the function call , Instead of defining the location of the function
That is, in its call location , Is a function call initiated through an object .
Implicit binding : object.fn()– It is necessary to have an object theme Call it up Function of This process is called implicit binding :
Implicit binding : It will automatically bind the object to the function this, Point to the caller
Kidnap three : According to the binding ;
Implicit binding has a prerequisite :
1. There must be a reference to the function inside the called object ( For example, an attribute );
2. If there is no such reference , On call , An error will be reported that the function cannot be found ;
3. It is through this reference , Indirect will this Bound to this object ;
call、apply、bind We explicitly bound this Object to point to , So it's called According to the binding
call、apply The first parameter is the same , Later parameters ,apply Is an array ,call For parameter list ;
If we want a function to always be bound to an object
bind The binding of will form a new function When called, the new function , The new function is always bound by our own specified binding this Object
Title binding four :new binding
JavaScript The function in can be used as the constructor of a class , That is to use new keyword
Use new Keyword to call a function is , Do the following :
1. Create a new object ;
2. This new object will be executed prototype Connect ;
3. This new object will be bound to the function call this On (this The binding of is completed in this step );
4. If the function does not return another object , The expression returns the new object ;
title this Interview question one
var name = "window";
var person = {
name: "person",
sayName: function () {
console.log(this.name);
},
};
function sayName() {
var sss = person.sayName;
sss(); // window: Independent function call
person.sayName(); // person: Implicit call
person.sayName(); // person: Implicit call (person.sayName)() In fact, it is equivalent to person.sayName();
(b = person.sayName)(); // window: Assignment expression ( Independent function call ) As long as it is an assignment expression -- Is the call of independent functions --- This kind of code will not be written in development
}
sayName();
title _this Interview question two
var name = "window";
var person1 = {
name: "person1",
foo1: function () {
console.log(this.name);
},
foo2: () => console.log(this.name),
foo3: function () {
return function () {
console.log(this.name);
};
},
foo4: function () {
return () => {
console.log(this.name);
};
},
};
var person2 = {
name: "person2" };
// person1.foo1(); // person1( Implicit binding )
// person1.foo1.call(person2); // person2( Show binding takes precedence over implicit binding )
// person1.foo2(); // window( The arrow function doesn't this, Do not bind scope , The upper scope is global )
// person1.foo2.call(person2); // window
// person1.foo3()(); // window( Independent function call )
// person1.foo3.call(person2)(); // window( Independent function call )-- The point is to look only at the final this Who is the binding , Here is the call of independent function
// person1.foo3().call(person2); // person2( The final call returns the function expression , The display binding is used )
// person1.foo4()(); // person1( Arrow functions are not bound this, Upper scope this yes person1)
// person1.foo4.call(person2)(); // person2( The upper scope is shown bound to a person2)
// person1.foo4().call(person2); // person1( The upper level found person1)person1.foo4() Make the upper scope person1, And because the arrow function doesn't this, No binding call, But from the upper scope
title _this Interview question three
var name = "window";
function Person(name) {
this.name = name;
(this.foo1 = function () {
console.log(this.name);
}),
(this.foo2 = () => console.log(this.name)),
(this.foo3 = function () {
return function () {
console.log(this.name);
};
}),
(this.foo4 = function () {
return () => {
console.log(this.name);
};
});
}
// Function every time new A new object will be created every time Every time it comes in this It is also customized according to the parameters you pass in this
var person1 = new Person("person1");
var person2 = new Person("person2");
person1.foo1(); // person1
person1.foo1.call(person2); // person2( Display is higher than implicit binding )
person1.foo2(); // person1 ( In the upper scope this yes person1)
person1.foo2.call(person2); // person1 ( In the upper scope this yes person1)
person1.foo3()(); // window( Independent function call )
person1.foo3.call(person2)(); // window
person1.foo3().call(person2); // person2
person1.foo4()(); // person1
person1.foo4.call(person2)(); // person2
person1.foo4().call(person2); // person1
var obj = {
name: "obj",
foo: function () {
},
};
title _this Interview question 4
var name = "window";
function Person(name) {
this.name = name;
this.obj = {
name: "obj",
foo1: function () {
return function () {
console.log(this.name);
};
},
foo2: function () {
return () => {
console.log(this.name);
};
},
};
}
var person1 = new Person("person1");
var person2 = new Person("person2");
person1.obj.foo1()(); // window Calls to independent functions
person1.obj.foo1.call(person2)(); // window person1.obj.foo1.call(person2) It's change foo1 Of this Pointing to person2 however foo1 The return value is an independent function The call of independent function is window
person1.obj.foo1().call(person2); // person2 Implicit binding person2
person1.obj.foo2()(); // obj Upper scope The upper scope of this function mainly depends on the return value foo2 Who transferred it Here is obj Retrieved
person1.obj.foo2.call(person2)(); // person2 Upper scope The upper scope of this function mainly depends on the return value foo2 adopt call change this Become person2 So it is person2
person1.obj.foo2().call(person2); // obj The upper scope is foo2 Become... By implicit binding obj, The arrow function doesn't this Look at the direction of the upper scope
//
// Understanding of the upper scope
// var obj = {
// name: "obj",
// foo: function() {
// The upper scope is global
// }
// }
// function Student() {
// this.foo = function() {
// The upper scope is function Student
// }
// }
边栏推荐
- Kingbases SQL language reference manual of Jincang database (16. SQL statement: create sequence to delete)
- Dio problem summary
- How to build a super interface collaboration platform: count the six weapons of apifox
- 【Pytorch进阶】pytorch模型的保存与使用
- Cookies and sessions
- LeetCode_回溯_中等_40.组合总和 II
- App Uploader下载安装
- 京东荣获中国智能科学技术最高奖!盘点京东体系智能技术
- 低代码工具有哪些特色?明眼人都能看出来的低代码两大发展轨迹!
- three. Two methods of making Earth annotation with JS
猜你喜欢

YGG 与 AMGI 的旗舰 NFT 项目 My Pet Hooligan 合作进入 The Rabbit Hole

任务二 kaggle糖尿病检测

Silent desktop fan chip dltap703sd Jericho

How to build a super interface collaboration platform: count the six weapons of apifox

7岁男童因下棋太快?机器人竟夹断其手指

I hope some suggestions on SQL optimization can help you who are tortured by SQL like me

DevSecOps,让速度和安全兼顾

EasyCVR设备管理列表页面,分页数据不显示的问题修复

Vs how to read data in MySQL (by the way, the problem of Chinese garbled code is solved through code)

打字比赛圆满结束!
随机推荐
go+mysql+redis+vue3简单聊室,第5弹:使用消息队列和定时任务同步消息到mysql
Is flush reliable? I just started to learn financial management. Is it safe to open a securities account?
STM32F103有源蜂鸣器驱动程序
【OBS】解决OBS推两个rtmp流 + 带时间戳问题
Use request header authentication to test API interfaces that need authorization
[Android] the black technology behind kotlin's rapid compilation. Learn about it~
Shell script basic programming commands
【机器学习】变量间的相关性分析
一文读懂 Kubernetes的四种服务类型!
猿辅导的科技硬实力:让AI从读懂孩子作业开始
ES6的方法&类数组转成真正的数组&判断数组的方法
2000 words to help you master anti shake and throttling
数组操作增,删,改,查
正则表达式
Intranet penetration learning (II) information collection
tf.GraphKeys
Impersonate authentication
shell脚本基础编程命令
C# 客户端程序调用外部程序的3种实现方法
试用了多款报表工具,终于找到了基于.Net 6开发的一个了