JS OOP: 04. Method di constructor function

by istiq20

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Method di constructor function</title>
</head>
<body>
    <script>
        function Person() {
            this.firstName = ""
            this.lastName = ""
            // method di constructor function
            this.sayHello = function (name) {
                console.log(`Hello ${name}, my name is ${this.firstName}`);
            }
        }

        // instance object otomatis punya method dari Person()
        const eko = new Person();
        const budi = new Person();

        console.log(eko);
        console.log(budi);
    </script>
</body>
</html>