12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <!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,4,5)
- xx.apply(person2,[5,6])
- //bind不会调用函数 返回的是一个修改this指向之后的方法
- xx.bind(person2,8,9)()
- </script>
- </body>
- </html>
|