JavaScript全局角度看this

<script charset="utf-8">
    //在没有进行任何初始化的时候,this就是window对象
     console.log(this);
    //构造函数或任何函数或在函数以外中出现的this,其实都指的是window
    this.document.write("大家好,我是使用的this.document.write显示的文字")
    function Caculator(){
        this.aaplus = function(op1,op2){
            console.log("相加的结果为" + (op1+op2));
        }
        this.aamul = function(op1,op2){
            console.log("相乘的结果为:" + op1 * op2)
        }
        aadivid = function(op1,op2){//如果不加this,也不加var的话,暗示为全局变量,即将该
            //方法名或变量名加到window中
            console.log("相除的结果为:" + op1 / op2)
        }
        var aasub = function(op1,op2){
            console.log("相减的结果为:"+ (op1 - op2))
        }
    }
    Caculator();
    aaplus(1,2);
    aadivid(2,2)
    //将把该函数中的带有this的属性和方法自动添加到对象中去,aadivid()方法没有this则该对象没有加到对象中
    var cal = new Caculator();//当构造函数前面有new关键字时,
    cal.aaplus(1,2);
    console.log(cal);
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值