123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- /*
- 区别
- 1.调用的方式不一样
- 2.call apply 直接调用修改后的方法 bind不会调用方法 返回的是一个方法 需要重新调用
- 修改this指向
- 1.call(修改的this,参数1,参数2)
- 2.apply(修改的this,[参数1,参数2])
- 3.bind(修改this,参数1,参数2)()
- */
- var person1 = {
- name: 'zs',
- age: 18,
- eat: function(){
- console.log(this)
- }
- }
- // person1.eat()
- var person2 = {
- name:'lisi',
- age: 30
- }
- person1.eat.call(person2)
- function xx(a,b){
- console.log(a,b,this)
- }
- xx(1,2)
- xx.call(person2,1,2)
- xx.apply(person2,[3,4])
- xx.bind(person2,7,8)()
- </script>
- </body>
- </html>
|