js中的this指向问题(小计)
this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定,this最终指向调用它的对象。
1.函数调用模式
当一个函数并非一个对象的属性时,那么它就是被当做函数来调用的。在此种模式下,this被绑定为全局对象,在浏览器环境下就是window对象;
function fn(){
var a = "hello";
console.log(this.a); //undefined
console.log(this); //window
}
fn();
2.方法调用模式
当函数被保存为一个对象的属性时,它就可称为这个对象的方法。当一个方法被调用时,this被绑定到这个对象上。如果调用表达式包含一个提取属性的动作(. 或 []),那么它被称为方法调用
var obj = {
name : "hello",
sayName : function(){
console.log(this.name); //hello
}
}
obj.sayName();
这里的this指向的对象是obj,因为调用这个sayName()函数是通过obj.sayName()执行的。
var obj = {
name : "hello",
obj2 : {
name : "world",
sayName : function(){
console.log(this.name); //world
console.log(this); //obj2
}
}
}
obj.obj2.sayName();
因为是obj.obj2调用的这个函数,所以指向obj2这个对象。
var obj = {
name : "hello",
obj2 : {
//name : "world",
sayName : function(){
console.log(this.name); //undefined
console.log(this); //obj2
}
}
}
obj.obj2.sayName();
同理,因为是obj.obj2调用的这个函数,所以指向obj2这个对象。
var name = "我是最外面的name";
var obj = {
name : "hello",
obj2 : {
name : "world",
sayName : function(){
console.log(this.name); //"我是最外面的name"
console.log(this); //window
}
}
}
var q = obj.obj2.sayName;
q();
q是全局变量,在全局环境下执行,this指向window;
3.构造函数调用模式
如果在一个函数前面加上new关键字来调用,那么就会创建一个连接到该函数的prototype成员的新对象,同时,this会被绑定到这个新对象上。这种情况下,这个函数就可以成为此对象的构造函数。
function fn(){
console.log(this);//fn
}
var a = new fn();
在构造函数,new出一个对象时,this指向这个构造函数,new关键字会改变this的指向;
function fn(){
this.name = "hello";
}
var a = new fn();
console.log(a.name); //hello
当用new关键字,返回的是一个对象,this指向的就是那个返回的对象;
如果返回的不是对象,this还是指向函数的实例,虽然null属于对象,但是返回null依然指向函数实例;
function fn(){
this.name = "hello";
return {};
}
var a = new fn();
console.log(a.name); //undefined
function fn(){
this.name = "hello";
return function(){
};
}
var a = new fn;
console.log(a.name); //undefined
function fn(){
this.name = "hello";
return function(){
};
}
var a = new fn;
console.log(a.name); //undefined
function fn(){
this.name = "hello";
return [];
}
var a = new fn;
console.log(a.name); //undefined
function fn(){
this.name = "hello";
return 1;
}
var a = new fn;
console.log(a.name); //hello
function fn(){
this.name = "hello";
return 1;
}
var a = new fn;
console.log(a.name); //hello
function fn(){
this.name = "hello";
return null;
}
var a = new fn;
console.log(a.name); //hello
function fn(){
this.name = "hello";
return undefined;
}
var a = new fn;
console.log(a.name); //hello
4.apply和call调用模式
JS中,函数也是对象,所有函数对象都有两个方法:apply和call,这两个方法可以让我们构建一个参数数组传递给调用函数,也允许我们改变this的值
var name = "window";
var obj = {
name : 'obj'
}
function sayName(){
console.log(this.name);
}
sayName();//window
sayName.apply(obj);//obj
sayName.call(obj);//obj
sayName.apply();//window
sayName.call(); //window
在全局范围内,this指向全局对象(浏览器下指window对象)
对象函数调用时,this指向当前对象
全局函数调用时,应该是指向调用全局函数的对象。
使用new关键字实例化对象时,this指向新创建的对象
当用apply和call上下文调用的时候指向传入的第一个参数;
js中的this指向问题(小计)的更多相关文章
- 理解js中this的指向
学习自原文 http://www.cnblogs.com/pssp/p/5216085.html后的一点小结(原文作者总结的很棒^_^)! 关于js中this的指向,在函数定义的时候还无法 ...
- js中改变this指向的call、apply、bind 方法使用
前言: 由于js 中this的指向受函数运行环境的影响,指向经常改变,使得开发变得困难和模糊,所以在封装sdk,写一些复杂函数的时候经常会用到this 指向绑定,以避免出现不必要的问题,call.ap ...
- js中this的指向
在js中this的指向对于新手来说一定是个难题,但是如果你真正理解了的话,也就没什么问题啦,下面就来讲讲this吧. JS中,this的值取决于调用的模式(调用对象),而JS中共有4种调用模式: 1. ...
- JS中的this 指向问题
我发现在对JS的学习中有很多朋友对this的指向问题还是有很大的误区或者说只是大致了解,但是一旦遇到复杂的情况就会因为this指向问题而引发各种bug. 对于之前学习过c或者是Java的朋友来说可能这 ...
- 轻松了解JS中this的指向
JS中的this指向一直是个让人头疼的问题,想当初我学的是天昏地暗,查了好多资料,看的头都大了,跟他大战了那么多回合,终于把它搞定个七八分,其实往往都是我们复杂化了,现在就让大家轻松看懂this的指向 ...
- js中 this 的指向
js中 this的指向一共存在3种地方: 1.全局的this; 2.构造函数的this; 3.call/apply; 一.全局的this: function test(){ this.d = 3;// ...
- 彻底理解js中this的指向,不必硬背。
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
- 了解学习JS中this的指向
[转] 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问 ...
- 彻底理解js中this的指向
首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...
随机推荐
- Java_打印从一个数到另一个数之间的整数,并每5个为一行
import java.util.Scanner; public class Dayin_100 { public static void main(String[] args) { System.o ...
- 关于pyCharm专业版的破解方法
用pycharm编写自动化脚本时,pycharm专业版的使用期限只有30天, 找到了pycharm破解方法. 破解码:{"licenseId":"145446792566 ...
- Azure Database for MySQL 报 Please specify SSL options and retry.
Exception has been thrown by the aspect of an invocation. ---> Authentication to host 'xxx.mysql. ...
- debian9安装mysql
cd /tmp wget https://dev.mysql.com/get/mysql-apt-config_0.8.7-1_all.deb dpkg -i mysql-apt-config_*.d ...
- MVC中EF代码优先问题
在练习Mvc项目时,提示如下数据库错误: The model backing the 'EFDbContext' context has changed since the database was ...
- 对于服务器AdminServer, 与计算机Machine-0相关联的节点管理器无法访问
控制台启动server时报"对于服务器server-1与计算机machin<!--StartFragment -->对于服务器AdminServer, 与计算机Machine-0 ...
- angular.identity()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- __http原理__01__通信流程_消息格式
本文转自 菜鸟教程 一.HTTP协议(HyperText Transfer Protocol,超文本传输协议) 是因特网上应用最为广泛的一种网络传输协议, 是用于从万维网(WWW:World Wi ...
- Web应用程序架构的比较
架构 技术优势 技术挑战 团队优势 团队挑战 单体 低延时 开发简单 没有重复的模型/验证 伸缩 由于代码库过大引起的复杂度 特性内沟通的开销低 失败的恐惧 特性间沟通的开销大 前端+后端 能够单独扩 ...
- Python函数中的列表
在看21天的Python书中写出了一个陷阱,但没给出解释,以下为代码陷阱