bind call

版权声明:互相学习,共同进步!! https://blog.csdn.net/hello_word2/article/details/84872474
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>

    var xiaoGang = {
        'userName': '小刚',
        'age': 18,
        'sex': '男',
        'getInfo': function () {
            console.log(this.userName + '==' + this.age + '===' + this.sex);
        }
    }
    //xiaoGang.getInfo();
    var xiaoHong = {
        'userName': '小红',
        'age': 20,
        'sex': '女'
    }
    //调用小刚的getInfo 显示 小红的信息
    //  xiaoGang.getInfo.call(xiaoHong);
    /**
     * 使用bind会返回一个新的函数
     * console.log(xiaoGang.getInfo.bind(xiaoHong));
     * ƒ () {
            console.log(this.userName + '==' + this.age + '===' + this.sex);
        }
     */
    xiaoGang.getInfo.bind(xiaoHong)();
    /**
     * 使用bind传参  test.bind(xiao)(1,2);
     */
    function test(x,y){
        console.log(this)
        console.log(this.userName)
        console.log(x);
        console.log(y);
    }
    var xiao= {
        'userName':'李白'
    }
    test.bind(xiao)(1,2);
</script>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/hello_word2/article/details/84872474