this指向who(下)

调用function时,常会在function内的一堆动作中看到this关键字,而this就是记录呼叫functionobject

昨天小复习:预设系结,this预设的是window;隐含系结,this绑定的是将function设定为method并呼叫他的object

呼叫function的方法 与 this指向的「呼叫function的物件」 (下)

四种系结

  • 预设系结
  • 隐含系结
  • 明确系结
  • new系结

明确系结(Explicit Binding)

  • 甚麽时候使用when :使用内建function → call , apply , bind呼叫function时
  • this跟谁绑who :
    • 第一个参数
    • 没有指定或填null:window(全域物件)

基本上call和apply一样,差在apply参数要放在伪数组里,call直接用逗号隔开
bind就差很多,bind回传一个function,而且他强制绑定一个物件,之後再变也没用

  • call 和apply 语法
    • function名.call(想绑定this的物件,参数1,参数2,...,参数n)
    • function名.apply(想绑定this的物件,[参数1,参数2,...,参数n])
var str = 'Here is
 Global',
		obj1={str:'Here is Obj1', log:logStr},
		obj2={str:'Here is Obj2'};
function logStr(){
	var str = 'Here is Local';
	console.log(this.str)
}

logStr.call(obj1);
logStr.call(obj2);
logStr.call(null);
logStr.call();

logStr.apply(obj1);
logStr.apply(obj2);
logStr.apply(null);
logStr.apply();

logStr();                         
var print = obj1.log;
print();
print.call(obj2);

var afterRunLog = obj1.log();
afterRunLog;

解析:

  • 倒数第二段

    • obj1log属性logStr function地址另给变数print,执行print
    • print()等价於logStr()
  • 最後一段
    最後一段只是为了比较var print = obj1.logvar afterRunLog = obj1.log()

    • 没有小括号,是把logStr function地址另给变数print
    • 有小括号,是把logStr function执行完後的结果,另给变数afterRunLog
  • apply通常用在参数不固定数量时

//要写一个不固定参数的add
function add(...x){
	var total =0;
	for(let i=0;i<x.length;i++){
		total = total + x[i]
	}
	return total
}

add.apply(null,[1,2,3,4,5]);
  • bind 回传一个强制绑定单一物件的function
var str = 'Here is Global',
		obj1={str:'Here is Obj1', log:logStr},
		obj2={str:'Here is Obj2'};
function logStr(){
	var str = 'Here is Local';
	console.log(this.str)
}

var logObj1 = logStr.bind(obj1);
logObj1();
logObj1.call(obj1);
logObj1.apply(obj2);
logObj1.call();

var dobBindObj2 = logObj1.bind(obj2);
dobBindObj2();
dobBindObj2.call(obj1);
dobBindObj2.apply(obj2);
dobBindObj2.call();

解析:

  • 倒数第二段
    • logStr.bind(obj1) 用bind绑定obj1,不管後面再用call , apply绑都没有用
  • 最後一段
    如果再用bind绑第二次,有办法改变this指向吗?
    这PART留给你试 程序码都打给你了,不试太可惜惹~~

new系结(Constructor Calls with new)

  • 甚麽时候使用when :用new呼叫function作为建立object的constructor
  • this跟谁绑who :新建立的object
var str = 'Here is Global';
function logStr(){
	var str = 'Here is Local';
	//小心要切换成"" 因为I'm
	//会报错Uncaught SyntaxError: Unexpected identifier
	this.str="Hi I'm New";
};

var objNew = new logStr();
console.log(objNew.str);

总结this的指向

  • 预设是window
  • 隐含是调用他的object
  • 明确是第一个参数
  • new是新建的object

参考资料

JavaScript - This (1) - iT 邦帮忙::一起帮忙解决难题,拯救 IT 人的一天
JS中this关键字详解


<<:  Leetcode 挑战 Day 17 [ 69. Sqrt(x) ]

>>:  [DAY12]就是要持久-Statefulset

【Day 18】jQuery DOM

何谓DOM? DOM = Document Object Model(文件物件模型) 根据MDN表示...

Day15. Inheritance & Super - Ruby 继承 part2

在 Day2 提到过,Ruby为单一继承的语言。若我们要实现多重继承的话,我们在 Day14 提到可...

一文说请什麽是GIS

地理资讯系统(英语:Geographic Information System,缩写:GIS) 这是...

DAY 22 Big Data 5Vs – Variety(速度) Kinesis (2)

接续介绍Kinesis家族中其它更实用的资料分析服务: 进入Kinesis服务首页可以看到这三个常常...

【Day 20】Python 一行内输入多个数字、多个字串及好用的刷题网站推荐

前言 前面介绍了那麽多语法,应该来实际使用看看了。要提升自己的程序能力有很多办法,刷题也是一种能让程...