JavaScript对象直接量里的this关键字

<html>
<head>
<title>新羿集团</title>
</head>
<script type='text/javascript'>
    function People()
    {        
        this.age=9;        
        
        //obj是对象直接量,在对象直接量中使用this关键字,this引用的不是调用对象,而是实例化的对象,和new Fun()的函数Fun里面this引用的需要实例化的对象一样
        var obj={SetAge:function(num){this.age=num;},GetAge:function(){return this.age;}};
        obj.SetAge(99);
        alert(obj.GetAge())
        //return obj;
    }

    var p=new People();
    alert(p.age);//如果People返回obj的话,弹出99,否则返回9.
    alert(window.age)//弹出undefined



    
</script>
<body>
<input type='text' name='userName'/>
<input type='text' name='password'/>
<input type='button' value='确认' onClick="myOpen();"/>
</body>
</html>
原文地址:https://www.cnblogs.com/mxw09/p/1766401.html