老师 这里为什么会输出这几个undefined可以给我讲一下吗

来源:3-3 编程练习

Dreamboat丶C

2022-04-02 23:26:15

<!DOCTYPE html>

<html lang="en">


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>


<body>

    <script>

        /*

            function Person(name, age) {

                this.name = name;

                this.age = age;

                this.say = function() {

                     console.log(this.name, this.age)

                }

            }

            Person.prototype.run = function() {

                console.log("run")

            }

            Person.intro = "this is a Person class"

            Person.show = function() {

                console.log('show')

            }

        */

        class Person {

            constructor(name,age) {

                this.name = name;

                this.age = age;

                this.say = function () {

                    console.log(this.name, this.age)

                }

            }

            run() {

                console.log("run")

            }

            static intro = "this is a Person class"

            static show = function () {

                console.log('show')

            }

        }

        const p=new Person('alex',18)

        console.log(p.run())

        console.log(Person.show())

        console.log(p.say())

        console.log(Person.intro)




    </script>

</body>


</html>

相关截图:

https://img.mukewang.com/climg/62486af30968654602930221.jpg

写回答

1回答

好帮手慕久久

2022-04-04

同学你好,修改后的class是对的,问题解答如下:

以console.log(p.run())为例,它的意思有两个:1、执行p.run方法;2、打印run方法的返回值:

https://img.mukewang.com/climg/624a4be709af985205980117.jpg

https://img.mukewang.com/climg/624a4c0b097c8f6806080126.jpg

由于run方法中,没有使用return设置返回值,所以默认返回undefined,因此会打印出undefined:

https://img.mukewang.com/climg/624a4c57094c066b05100180.jpg

其余同理。

祝学习愉快!

0

0 学习 · 17877 问题

查看课程