joken-前端工程师

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    var other={
        abc:'other-abc'
    }


    function demo(){
        this.abc='sfsdfsdf'
        this.cbd='4645'
    }
    demo.prototype.fnOne=function(){
        console.log(this.abc,"abc")
        console.log(arguments,"argument")
    }
    demo.prototype.fntwo=function(){
        console.log(this.cbd,"sdlkfjsdkfj")
    }
    var mydemo=new demo;
    mydemo.fnOne.apply(other,['cshu1','cshu2'])
</script>
<script>
    console.log('====================')
    function testApply(){
        console.log(this.abc,"apply")
        console.log(arguments,"arguments")
    }
    var thisTar={
        abc:'sdfsdfsf'
    }
    testApply.apply(thisTar,[234,234234])
</script>
</html>

区别

js apply 和 call 基本上没什么区别,主要的区别是传输参数的不同
apply(tar,[参数...]) #通过数组传递参数
calll(tar,'参数1','参数2'....)

bind 也是用来绑定this的指向,只是和apply\call 的区别是bind不会执行函数。

posted on 2020-05-30 18:11  joken1310  阅读(64)  评论(0)    收藏  举报