新手,关于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。

推荐问题