js 函数与类的区别

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

</body>
<script>

    function show() {

        console.log(this)
    }

    show(); //window object
    new show(); //object



    j = {
        name:'ldq',
        age:18,
        info:function () {
            console.log(`我的名字是${name},我的年龄是${age}`)
            console.log(123)
        }
    };
    console.log(j);
    new j.info();



    function show(name,age) {
        //系统偷偷替我们做了
//        var this = new object();

        this.name = name;
        this.age = age;
        this.fun1=function () {
            console.log(`我的名字是${this.name},我的年龄是${this.age}`)

            // /系统偷偷替我们做了
//            return this

        }
    }
    obj = new show('ldq',18)
    obj.fun1()
</script>
</html>
原文地址:https://www.cnblogs.com/ldq1996/p/8371073.html