美文网首页
2019-07-16

2019-07-16

作者: 沧海月明_9d1b | 来源:发表于2019-07-16 17:42 被阅读0次

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();    //姓名: 李四 ,年龄:18

var newObj = {
    name: '赵五',
    age: 20
};

obj.output.call( newObj );  // 姓名:  赵五  ,年龄:20
obj.output.apply( newObj );  // 姓名:  赵五  ,年龄:20
obj.output.bind( newObj )();  // 姓名:  赵五  ,年龄:20 

相关文章

网友评论

      本文标题:2019-07-16

      本文链接:https://www.haomeiwen.com/subject/rlmtlctx.html