改变this指向

   // // 1.普通函数this指向window

    // function log1(){

    //     console.log(this);

    // }

    // log1()

    // // 2.构造函数this指向实例化对象

    // function Person(age,name){

    //     this.name = name;

    //     this.age = age;

    //     this.say = function(){

    //         console.log(this.age);

    //     }

    // }

    // var  per1 = new Person(13,"zs")

    // per1.say();

    // // 3.对象函数this指向当前对象

    // var per2 =  {

    //     age:77,

    //     name:"ls",

    //     say:function(){

    //         console.log(this.name);

    //     }

    // }

    // per2.say()

    // // 4.事件函数this指向事件源

    //     (1)、call方法

    // 1、call()方法可以进行普通函数的调用

    // function loger() {

    //     console.log(3333);

    //     console.log(this);

    // }

    // loger.call()



 

    // 2、call()方法可以改变this的指向,如果没有参数,this指向window

    // 3、call()方法可以改变this的指向,如果有一个参数,this指向该参数

    // window.name = '我是windowname'

    // var  per3 = {age:28,name:'喜羊羊',say:function(){

    //     console.log("名字"+this.name);

    // }}

    // var  per4 = {age:6,name:'懒羊羊'}

    // log1.call(per3)

    // per3.say()


 

    // per3.say.call()

    // per3.say.call(per4)


 

    // 4、call()方法可以改变this的指向,如果有多个参数,this指向第一个参数,剩下的是个参数列表(构造函数继承的案例


 

    // function addn(a,b,c){

    //     console.log(this);

    //     console.log(a+b+c);

    // }

    // addn(1,2,3)

    // addn.call(per3,1,2,3)



 

    // // 、apply方法

    // // 1、 apply()方法可以进行普通函数的调用

    // // 2、apply()方法可以改变this的指向,如果没有参数,this指向window

    // // 3、apply()方法可以改变this的指向,如果有一个参数,this指向该参数

    // window.name = '我是windowname'

    // var  per3 = {age:28,name:'喜羊羊',say:function(){

    //     console.log("名字"+this.name);

    // }}

    // var  per4 = {age:6,name:'懒羊羊'}

    // per3.say()

    // per3.say.apply()

    // per3.say.apply(per4)

    // //  4、apply()方法可以改变this的指向,如果有多个参数,this指第一个参数,可以为null或者window,第二个参数是数组


 

    // function addn(a,b,c){

    //     console.log(this);

    //     console.log(a+b+c);

    // }

    // addn(1,2,3)

    // addn.apply(per3,[2,3,4])

    var per = {

        name:'懒羊羊',

        getName:function(a,b,c){

            console.log(this.name);

            console.log(a,b,c);

        }

    }

    var per2 = {

        name:'喜羊羊',

       

    }

    // bind 会返回一个函数 ,如果要调用  记得加在加小括号

    per.getName.bind(per2,2,3,4)()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值