js 调用类实例的方法返回函数对象并直接运行返回的函数报错

代码如下:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
        window.onload=function(){
            x="this=window";
            var a={
                x:"this=a",
                kk:function(){
                    console.log(this.x);
                     return function(){console.log(this.x);};
                    }
            }
            //alert(a.kk);
            (a.kk()());
        }
    </script>
</head>
<body>

</body>
</html>

一段很短的代码,去掉注释可以运行,但是不去掉注释就会报错
function.html:18 Uncaught TypeError: a.kk(...) is not a function(…)

分号加上,要不就是对象{}当方法执行,此时你传入的参数a还没有定义

         window.onload = function () {
            x = "this=window";
            var a = {
                x: "this=a",
                kk: function () {
                    console.log(this.x);
                    return function () {
                        console.log(this.x);
                    };
                }
            };//////////////////////////////////////////////
            //alert(a.kk);
            (a.kk()());
        }