zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Ruby自学笔记(二)— Ruby的一些基础知识
    Ruby自学笔记(一)— 基本概况
    Tomcat基础教程(四)
    Excel导入
    构建API
    序列化
    图片上传(练习)
    发邮件
    发短信
    Excel表导出
  • 原文地址:https://www.cnblogs.com/mxw09/p/1766401.html
Copyright © 2011-2022 走看看