美文网首页
call applay bind的用法

call applay bind的用法

作者: 五四青年_4e7d | 来源:发表于2021-01-13 17:47 被阅读0次

    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>
    

    相关文章

      网友评论

          本文标题:call applay bind的用法

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