Javascript의 Object 생성 방법은 세 가지가 있다

  1. new Object()이용
  2. 리터럴 표기법
  3. 프로토타입
  1. new Object()이용
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
            <script>
                function deposit(a) {return this.balance += a;}
                function withdraw(a) {return this.balance -= a;}
     
            </script>
        </head>
        <body>
            <h3>new Object()</h3>
            <hr>
            <script>
                var account = new Object();
                account.owner = 'KSD';
                account.code = '1111';
                account.balance = 400000;
                account.deposit = deposit;
                account.withdraw = withdraw;
     
                account.deposit(10000);
                document.write('after deposit balance = ' + account.balance + '<br>');
                account.withdraw(20000);
                document.write('after deposit withdraw = ' + account.balance + '<br>');
            </script>
        </body>
    </html>
    cs
  2. 리터럴 표기법
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
        </head>
        <body>
            <h3>new Object()_Literal</h3>
            <hr>
            <script>
                var account = {
                owner : 'KSD',
                code : '1111',
                balance : 400000,
                deposit : function deposit(a) {return this.balance += a;},
                withdraw : function withdraw(a) {return this.balance -= a;}
                }
     
                account.deposit(10000);
                document.write('after deposit balance = ' + account.balance + '<br>');
                account.withdraw(20000);
                document.write('after deposit withdraw = ' + account.balance + '<br>');
            </script>
        </body>
    </html>
    cs
  3. 프로토타입
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <html>
        <head>
            <title></title>
            <meta charset="utf-8">
        </head>
        <script>
            function Account(owner, code, balance){
     
                this.owner = owner;
                this.code = code;
                this.balance = balance;
                this.deposit = function deposit(a) {return this.balance += a;}
                this.withdraw = function withdraw(a) {return this.balance -= a;}
     
            }
        </script>
        <body>
            <h3>new Object()_Prototype</h3>
            <hr>
            <script>
                var account = new Account('KSD''1111'40000);
            account.deposit(10000);
            document.write('after deposit balance = ' + account.balance + '<br>');
            account.withdraw(20000);
            document.write('after deposit withdraw = ' + account.balance + '<br>');
            </script>
        </body>
    </html>
    cs

     

실행결과 :

+ Recent posts