call applay bind的用法

call()

image.png

applay()

image.png

bind()

image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>单利模式应用</title>
</head>

<body>
</body>
</html>
<script>

    //call
    function stdunt(n1, n2) {
        console.log(this.name)        //this=>objs
        console.log(this.name + n1 + n2)  //this=>8
    }

    let objs = {
        name: 3
    }

    stdunt.call(objs, 2, 3)




    //applay
    function hello(name, age) {
        console.log(this)
        console.log(name);
        console.log(age + this.name);
        console.log(arguments)
    }

    let util = {
        name: 234
    }

    hello.apply(util, ["tsrot", 24, 12, 45, 6]);



    //bind
    var person = {
        name: "tsrot",
        age: 24,
        sayHello: function (age) {
            console.log(this.name);
            console.log(age);
        }
    };
    var son = {
        name: "xieliqun"
    };
    var boundFunc = person.sayHello.bind(son);

    boundFunc(25)





</script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容