์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผํ…๋ฐ์š” ๊ฐ์ฒด๊ฐ€ ๋ฌด์—‡์ธ์ง€๋Š” ์•Œ๊ณ ๊ณ„์‹ ๋‹ค๋Š” ์ „์ œํ•˜์— ์‹ค๋ฌด ์œ„์ฃผ์˜ ๋‚ด์šฉ์„ ์–˜๊ธฐํ•ด๋ณด๋„๋ก ํ• ๊ฑฐ์—์š”. ์ž๋ฐ”์˜ ๊ฐ์ฒด์™€ ๋™์ผํ•œ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ณ  ๋‹จ์ง€ ์‚ฌ์šฉ๋ฒ•์ด ์–ด๋–ค์ง€์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€๋Š” ์ •๋„๋กœ ์Šต๋“ํ•˜์‹œ๋ฉด ๋ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

๊ฐ์ฒด์˜ ์†์„ฑ

์ž๋ฐ”๋ฅผ ๊ณต๋ถ€ํ•ด ๋ณด์‹ ๋ถ„๋“ค์ด๋ผ๋ฉด ์•„์‹œ๊ฒ ์ง€๋งŒ ๊ฐ์ฒด ์•ˆ์—๋Š” ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋‚˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋“ค์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์†์„ฑ์ด๋ผ๊ณ  ์–˜๊ธฐ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์†์„ฑ๋“ค์— ๊ฐ’์„ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.:
๊ฐ์ฒด๋ช….์†์„ฑ๋ช… = ์†์„ฑ๊ฐ’;

์˜ˆ์ œ:

์•„๋ž˜ ์˜ˆ์ œ๋Š” document๊ฐ์ฒด์˜ title์ด๋ผ๋Š” ์†์„ฑ์„ str๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. 

var str = document.title;

๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ

์ด๋ฒˆ์—๋Š” ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ฃ .

์˜ˆ์ œ:

์•„๋ž˜ ์˜ˆ์ œ๋Š” document๊ฐ์ฒด ๋‚ด๋ถ€์— ์„ ์–ธ๋˜์–ด์žˆ๋Š” write()๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ ์—ฌ๋Ÿฌ๋ฒˆ ๋ณด์…จ์„ ๊ฒ๋‹ˆ๋‹ค. hello world ๋ญ ์ด๋Ÿฐ ์˜ˆ์ œ์—์„œ ๋ง์ด์ฃ .

document.write("This is test");


์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด

๋‹ค ์•„์‹œ๊ฒ ์ง€๋งŒ ๋ชจ๋“  ์‚ฌ์šฉ์ž์ •์˜ ๊ฐ์ฒด์™€ ์ด๋ฏธ ์ •์˜๋˜์–ด์žˆ๋Š” built-in ๊ฐ์ฒด๋“ค์€ Object๋ผ๋Š” ๊ฐ์ฒด์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์ž…๋‹ˆ๋‹ค.

The new Operator:

์•„๋ž˜ ์˜ˆ์ œ๋Š” new ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ built-in JavaScript ๊ฐ์ฒด๋“ค์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.

var employee = new Object();
var books = new Array("C++", "Perl", "Java");
var day = new Date("August 15, 1947");


Object() ์ƒ์„ฑ์ž:

์ƒ์„ฑ์ž๋ผ๋Š” ๊ฒƒ์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ new Object(); ์˜ Object()๋Š”  Object๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ var๋กœ ์„ ์–ธํ•œ employee์— ํ• ๋‹นํ•ด์ฃผ๋ฉด ๋‚˜์ค‘์— ์ด ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ๋•Œ employee๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ employee๊ฐ€ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ์ด๋Ÿฐ ๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์„ ๊นŒ์š”? ์†์„ฑ์€ ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ varํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜์ฃ . 

์˜ˆ์ œ 1:

์•„๋ž˜ ์˜ˆ์ œ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค.

<html>
<head>
<title>User-defined objects</title>
<script type="text/javascript">
var book = new Object();   // Create the object
    book.subject = "Perl"; // Assign properties to the object
    book.author  = "Mohtashim";
</script>
</head>
<body>
<script type="text/javascript">
   document.write("Book name is : " + book.subject + "<br>");
   document.write("Book author is : " + book.author + "<br>");
</script>
</body>
</html>

 

 

์˜ˆ์ œ 2:

์ด๋ฒˆ ์˜ˆ์ œ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ this ํ‚ค์›Œ๋“œ๋Š” ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

<html>
<head>
<title>User-defined objects</title>
<script type="text/javascript">
function book(title, author){
    this.title = title; 
    this.author  = author;
}
</script>
</head>
<body>
<script type="text/javascript">
   var myBook = new book("Perl", "Mohtashim");
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
</script>
</body>
</html>

 

 

๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ ์ •์˜ํ•˜๊ธฐ

์ด๋ฒˆ์—๋Š” ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ:

<html>
<head>
<title>User-defined objects</title>
<script type="text/javascript">

// ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋  ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
function addPrice(amount){
    this.price = amount; 
}

function book(title, author){
    this.title = title; 
    this.author  = author;
    this.addPrice = addPrice; // ๋ฉ”์†Œ๋“œ๋ฅผ ์†์„ฑ์œผ๋กœ ํ• ๋‹น(์„ ์–ธ)ํ•ฉ๋‹ˆ๋‹ค.
}

</script>
</head>
<body>
<script type="text/javascript">
   var myBook = new book("Perl", "Mohtashim");
   myBook.addPrice(100);
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
   document.write("Book price is : " + myBook.price + "<br>");
</script>
</body>
</html>

 

 

with ํ‚ค์›Œ๋“œ

with ํ‚ค์›Œ๋“œ๋Š” ๊ฐ์ฒด์˜ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ์œ„ํ•œ ๋‹จ์ถ•ํ‚ค์›Œ๋“œ ๊ฐ™์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

with์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด๋Š” ํ•ด๋‹น ๋ธ”๋ฝ๋‚ด์—์„œ ๊ธฐ๋ณธ์ ์ธ ์ฐธ์กฐ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.๋”ฐ๋ผ์„œ ํ•ด๋‹น ๊ฐ์ฒด์˜ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผํ•  ๋•Œ ๊ถ‚์ด ํ•ด๋‹น ๊ฐ์ฒด์ด๋ฆ„๊ณผ ์ ์„ ์•ˆ์ฐ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•:

with (object){
    properties used without the object name and dot
}

์˜ˆ์ œ:

<html>
<head>
<title>User-defined objects</title>
<script type="text/javascript">

// Define a function which will work as a method
function addPrice(amount){
    with(this){
       price = amount; 
    }
}
function book(title, author){
    this.title = title; 
    this.author  = author;
    this.price = 0;
    this.addPrice = addPrice; // Assign that method as property.
}
</script>
</head>
<body>
<script type="text/javascript">
   var myBook = new book("Perl", "Mohtashim");
   myBook.addPrice(100);
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
   document.write("Book price is : " + myBook.price + "<br>");
</script>
</body>
</html>

 

 

 

 

 

 Reference : http://www.tutorialspoint.com/javascript/javascript_objects.htm