JavaScript-对象增删改查

增加(C)

方式一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let person = new Person();
        
        person.name = "BNTang";
        person.say = function () {
            console.log("hello world");
        }

        console.log(person);
    </script>
</head>
<body>

</body>
</html>

方式二

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let person = new Person();

        person["name"] = "zs";
        person["say"] = function () {
            console.log("hello world");
        }

        console.log(person);
    </script>
</head>
<body>

</body>
</html>

删除(R)

方式一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let p = new Person();

        delete p.name;
        delete p.say;

        console.log(p);
    </script>
</head>
<body>

</body>
</html>

方式二

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let p = new Person();

        delete p["name"];
        delete p["say"];

        console.log(p);
    </script>
</head>
<body>

</body>
</html>

修改(U)

方式一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let p = new Person();

        p.name = "BNTang";
        p.say = function () {
            console.log("hi");
        }

        p.say();
        console.log(p);
    </script>
</head>
<body>

</body>
</html>

方式二

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let p = new Person();

        p["name"] = "ww";
        p["say"] = function () {
            console.log("hi");
        }

        p.say();
        console.log(p);
    </script>
</head>
<body>

</body>
</html>

查询(D)

方式一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let p = new Person();

        p.name = "BNTang";
        p.say = function () {
            console.log("hi");
        }

        console.log(p.name);
    </script>
</head>
<body>

</body>
</html>

方式二

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script>
        class Person {
        }

        let p = new Person();

        p.name = "BNTang";
        p.say = function () {
            console.log("hi");
        }

        console.log(p["name"]);
    </script>
</head>
<body>

</body>
</html>

posted @ 2021-10-17 15:21  BNTang  阅读(70)  评论(0编辑  收藏  举报