JavaScript-属性方法分类

在 JavaScript 中属性和方法分类两类

实例属性与实例方法

在企业开发中通过实例对象访问的属性,我们就称之为 实例属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        function Person() {
            this.name = "BNTang";
            this.say = function () {
                console.log("hello world");
            }
        }

        let person = new Person();
        console.log(person.name);
    </script>
</head>
<body>
</body>
</html>

在企业开发中通过实例对象调用的方法,我们就称之为 实例方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        function Person() {
            this.name = "BNTang";
            this.say = function () {
                console.log("hello world");
            }
        }

        let person = new Person();
        person.say();
    </script>
</head>
<body>
</body>
</html>

静态属性与静态方法

在企业开发中通过构造函数访问的属性,我们就称之为 静态属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        function Person() {
            this.name = "BNTang";
            this.say = function () {
                console.log("hello world");
            }
        }

        // 构造函数也是一个"对象", 所以我们也可以给构造函数动态添加属性和方法
        Person.num = 666;
        console.log(Person.num);
    </script>
</head>
<body>
</body>
</html>

在企业开发中通过构造函数调用的方法,我们就称之为 静态方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        function Person() {
            this.name = "BNTang";
            this.say = function () {
                console.log("hello world");
            }
        }

        Person.run = function () {
            console.log("run");
        }
        Person.run();
    </script>
</head>
<body>
</body>
</html>
posted @ 2021-09-06 14:40  BNTang  阅读(52)  评论(0编辑  收藏  举报