javascript——this用法

<!DOCTYPE html>
<html>
<head>
    <title>javascript_this用法</title>
    <meta charset="UTF-8">
    <script type="text/javascript">
        /**
         * 当需要创建一个类的时候
         * 设置类的属性和方法需要通过
         * this关键字来引用
         * 但是需要特别注意:
         * this关键字在调用
          */
        var color="red";
        function showColor(){
            alert(this.color);
        }
        //创建一个类
        function Circle(color){
            this.color=color;
            this.showColor=showColor;
        }
        var c=new Circle("yellow");
        //使用c调用showColor方法,等于调用showColor()
        //此时的this是c,所以color是yellow
        c.showColor();
        showColor();//red



    </script>
</head>
<body>

</body>
</html>

  

posted on 2015-02-07 22:41  aicpcode  阅读(119)  评论(0编辑  收藏  举报

导航