关于javascript 中的this 关键字的用法demo小代码

来源:互联网 发布:java中有多继承吗 编辑:程序博客网 时间:2024/05/16 07:57

转载自                       ---------------->   http://www.cnblogs.com/justany/archive/2012/11/01/the_keyword_this_in_javascript.html



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>this_test</title>
        <meta name="author" content="Administrator" />
        <!-- Date: 2015-01-04 -->
    </head>
    <body>

        <script type="text/javascript">
            /*
             //demo_partone
             var name = "Bob";

             var nameObj =
             {
             name : "Tom",
             showName : function()
             {
             alert(this.name);
             },

             waitShowName : function()
             {

             setTimeout(this.showName, 1000);
             }

             // 由于setTimeout 和setTimeout函数是通过window全局对象调用
             // 所以得到结果位Bob
             // 若要得到  Tom结果
             // 这么写
             // waitShowName : function()
             // {
             // var that = this;
             // setTimeout(function()
             // {
             // that.setTimeout();
             // },1000);
             // }

             };

             nameObj.waitShowName();
             */

            /* demo_parttwo

             var someone =
             {
             name : "Bob",
             showName : function()
             {
             alert(this.name);
             }
             };

             var other =
             {
             name : "Tom",

             showName : someone.showName
             };

             other.showName();
             */

            /* demo_partthree
             var name = "Tom";

             var Bob =
             {
             name : "Bob",
             show : function()
             {

             alert(this.name);
             }
             };

             var show = Bob.show;
             show();
             */
            /*

             demo_partfour
             var name = "window";

             var Bob =
             {
             name : "Bob",

             showName : function()
             {
             alert(this.name);
             }
             };

             var Tom =
             {
             name : "Tom",
             showName : function()
             {

             var fun = Bob.showName;

             fun();
             }
             };

             Tom.showName();

             */
            /*

             //demo_partfive
             var name = "Bob";
             var nameObj =
             {
             name : "Tom",
             showName : function()
             {
             alert(this.name);
             },
             waitShowName : function()
             {
             var that = this;
             setTimeout("that.showName();", 1000);
             }
             };

             nameObj.waitShowName();
             */
            /*
             var fun = new Function("alert(this)");
             fun();

             */
        </script>

    </body>
</html>

-===================================================************************************************* 这是一条分割线



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>this_test2</title>
        <meta name="author" content="Administrator" />
        <!-- Date: 2015-01-04 -->
    </head>
    <body>
        
        
        <!-- <button id="box" name="box">
            Click me!  此段脚本段给IE用   
        </button>

        <script >
            var name = "window";

            function showName()
            {
                alert(this.name);
            }
            
            document.getElementById("box").attachEvent("onclick",showName);
            

        </script> -->
        
        
        <!-- <button id="box" name="box">Click Me</button>
        
        <script>    //此段脚本段给 非IE
            var name = "window";
            
            function showName()
            {
                alert(this.name);
            }
            
            document.getElementById("box").addEventListener("click",showName,false);
            
            
        </script> -->
    </body>
</html>



0 0
原创粉丝点击