新手,关于this的问题!

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    

    <script>
            function Person(name,age){
                console.log(this);
                this.name = name;
                this.age = age;
                this.getName = function(){
                    console.log(this.name);
                }
                this.getage = function(){
                    console.log(this.age);
                }
            }
            
            Person();
            function Student(name,age,num){
                Person.call(this,name,age);
            }
            var p1 = new Person("xyf",18);
            //p1.getName();
    </script>
    </body>
</html>
阅读 1.6k
1 个回答

JavaScript中,this是当前对象。
你的代码有问题,输出的 undefined。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题