zno2

js 中的 this

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    var tool = {
        a : 'some msg',
        show : function() {
            alert(this.a);
        }
    }

    tool.show();

    var tool2 = {
        a : 'msg1',
        b : {
            a : 'msg2',
            show : function() {
                alert(this.a);
            }
        }
    }

    tool2.b.show();
</script>
</head>
<body>

</body>
</html>

 

用浏览器打开,分别弹出消息

some msg

msg2

 

js中没有super关键字,this关键字代表:

this 只能出现在function中,且这个function对应的key所在的 对象就是this

posted on 2023-06-01 16:56  zno2  阅读(5)  评论(0编辑  收藏  举报

导航