javascript的this关键字面试详解大全编程语言
一直感觉 javascript 中的 this 是一个大坑,虽然一直都有注意,一直都有研究,但是总是会忘记。在这里,总结一下一些简单的规律
一:直接调用的都是window
除非是 bind 绑定过的 ,其他情况下,直接调用的方法的 this 都是 window 。所谓的直接调用,就是直接以 method() 的形式调用,没有 call , apply , new
看几种情况:
function foo(){
return this;
}
foo(); // window
var obj = {
foo: function(){ return this; }
}
var foo = obj.foo;
foo(); // window
function Foo(){
this.foo = function(){
console.log(this);
}
var foo = this.foo;
foo(); // window
}
new Foo();
二:谁调用,谁是this
除非是 bind 绑定过的 ,一般情况下,谁调用这个方法,那么内部的 this 就指向这个对象。也就是说 obj.method() ,那么就指向 obj 。 obj.foo.method() ,那么就指向 obj.foo
看几个例子:
var obj = {
foo: function(){ return this; }
}
obj.foo(); // obj调用,所以结果是obj
function foo(){ return this };
var obj = {};
obj.foo = foo;
obj.foo(); // obj调用,所以结果是obj
var obj = {
bar: function(){ return this; },
foo: function(){
return this.bar();
}
}
obj.foo(); // 在foo中, this是obj, 而this调用的bar, 所以返回的是obj
var obj = {
bar: {
foo: function(){ return this }
}
}
obj.bar.foo(); // obj.bar调用的foo,所以返回的结果是bar
function foo(){
this.bar = function(){ return this }
return this.bar();
}
foo(); // 由于foo中的this是window, 所以this.bar()返回的是window
function Foo(){
this.foo = function(){ console.log(this); }
this.foo();
}
var object = new Foo(); // 由于this.foo中的this是object,所以this是object
三:new会生成一个新的this
所有情况下,(箭头函数不能使用 new 关键字),使用了 new 以后,会把内部的 this 指向新生成的对象。
除去bind的情况下, prototype 中的 this 也指向新生成的对象
function Foo(){
console.log(this); // this指向新生成的对象,object
}
var object = new Foo();
function Foo(){
this.foo = function(){ return this === object; }
}
var object = new Foo();
object.foo(); // 输出true
function Foo(){}
Foo.prototype.foo = function(){ return this === object; }
var object = new Foo();
object.foo(); // 输出true
四:call, apply是谁,this就是谁
除非是bind的情况, call , apply 是谁,那么内部的 this 就是谁。
注意:如果是基本类型,那么 javascript 会把基本类型转换成 Object 的形式
也是看例子:
function foo(){ return this; }
foo.call( a ); // String
typeof foo.call( a ); // object
var obj = {
foo : function(){ return this; }
}
obj.foo.call(1); // Number
typeof obj.foo.call(1); // object
function Foo(){
this.foo = function(){ return this; }
}
var object = new Foo();
object.foo.call(1); // Number
五:bind是谁,this就是谁
除了 new 这一种特殊情况 , bind 的对象是谁,那么 this 也就是谁。即使 call , apply 也没有权利改变。
注意:如果是基本类型,那么 javascript 会把基本类型转换成 Object 的形式
function foo() { return this; }
foo = foo.bind(1);
foo(); // Number
typeof foo(); // object
function foo() { return this; }
foo = foo.bind(1);
foo.call( a ); // Number 1
function Foo() { return this; }
Foo.prototype.foo = (function(){ return this; }).bind(1);
var object = new Foo();
object.foo(); // Number
特殊情况
new 这个关键词可以改变方法内部的 this ,使他指向新生成的对象
function Foo(){ this.foo = function(){ console.log(this === obj) } }
Foo = Foo.bind(1);
var obj = new Foo();
obj.foo(); // 输入true
六:箭头函数的 this 是根据定义环境的 this 来定的,也就是说定义的函数周围的 this 是什么,它的 this 就是什么。
而且不会被 bind , call , apply 所改变
var foo = ()= { return this };
foo() // window
var obj = { foo: ()= this }
obj.foo(); // 由于定义的时候,周围的环境是window,所以返回window
var obj = {
foo(){
var bar= ()= { return this };
return bar();
}
}
obj.foo(); // 由于定义bar的时候,周围环境是obj,所以返回obj
var foo = obj.foo;
foo(); // 同理,这里是window
var foo = ()= { return this };
foo = foo.bind(1);
foo(); // window
foo.call(1); // window
foo.apply(1); // window
function Foo(){
// 箭头函数
var a = ()= {
console.log(this === object); // true
}
// 对比普通函数
var b = function(){
console.log(this === window); // true
}
this.foo = function(){
a(); b();
}
}
var object = new Foo();
object.foo();
function Foo(){}
// window
Foo.prototype.foo = ()= { return this }
// window
var object = new Foo();
object.foo(); // 由于定义foo的时候,周围环境是window,所以这里是window
七:由于只是我根据经验和资料自己总结的,所以不知道是否有疏忽或者遗漏,如果有问题的地方,欢迎提出!
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14966.html
cjava相关文章
- javascript 高级教程 视频_精通JavaScript
- javascript 基础_JavaScript高级编程
- 【说站】javascript中yield关键字的使用
- JavaScript中this指向哪儿?如何确定this?-前端面试进阶
- JavaScript刷LeetCode拿offer-js版字典
- JavaScript 框架太多了?相反,是太少了
- 使用JavaScript写的一个旋转的彩圈详解编程语言
- JavaScript学习总结(二十)——Javascript非构造函数的继承详解编程语言
- javascript面试常用知识点汇总详解编程语言
- javascript实现轮显新闻标题链接
- JavaScript中Math对象使用说明
- JavaScript在IE和Firefox上的差异及相互替代的实现方法
- Javascript与flash交互通信基础教程
- JavaScript入门教程(5)jsScreen屏幕对象
- 选择指定数量后checkbox不可选(变灰)javascript代码
- Javascript阻止javascript事件冒泡,获取控件ID值
- javascript写类方式之九
- Javascript实现TreeViewCheckBox全选效果
- JavaScript中apply与call的用法意义及区别说明
- javascript对象入门实例教程
- 最短的javascript:地址栏载入脚本代码
- JavaScript高级程序设计阅读笔记(十六)javascript检测浏览器和操作系统-detect.js
- javascript中String.match()与RegExp.exec()的区别说明
- Javascript算符的优先级介绍
- DojoJavascript编程规范规范自己的JavaScript书写
- 用javascript读取xml文件读取节点数据
- javascript中sort()的用法实例分析