this,call()、apply()、bind() 的混合用法
https://www.runoob.com/w3cnote/js-call-apply-bind.html
https://blog.csdn.net/marcelwu/article/details/78934069
例1
var name = '张三', age = 16;
/*
* 'use strict';// this的值就是undefined
*非严格模式,this就是windowd
*/
console.log('姓名:' + this.name + ',年龄:' + + this.age); //姓名:张三,年龄:16
var obj = {
name: '李四',
age: 18,
output : function () {
console.log('姓名:' + this.name + ',年龄:' + + this.age);
}
};
/*
* obj作用域之内,this值就是obj对象
*/
obj .output(); //姓名: 李四 ,年龄:18var newObj = {
name: '赵五',
age: 20
};obj.output.call( newObj ); // 姓名: 赵五 ,年龄:20
obj.output.apply( newObj ); // 姓名: 赵五 ,年龄:20
obj.output.bind( newObj )(); // 姓名: 赵五 ,年龄:20
网友评论