πŸ’» Programming/Javascript

[Javascript / μžλ°”μŠ€ν¬λ¦½νŠΈ] κ°•μ’Œ #16 - Object ( 객체 )

μΌ€μ΄μΉ˜ 2015. 8. 5. 17:06

이번 ν¬μŠ€νŠΈμ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 객체에 λŒ€ν•΄μ„œ μ•Œμ•„λ³Όν…λ°μš” 객체가 λ¬΄μ—‡μΈμ§€λŠ” μ•Œκ³ κ³„μ‹ λ‹€λŠ” μ „μ œν•˜μ— 싀무 μœ„μ£Όμ˜ λ‚΄μš©μ„ μ–˜κΈ°ν•΄λ³΄λ„λ‘ ν• κ±°μ—μš”. μžλ°”μ˜ 객체와 λ™μΌν•œ 것이라고 μƒκ°ν•˜μ‹œλ©΄ 되고 단지 μ‚¬μš©λ²•μ΄ 어떀지에 λŒ€ν•΄μ„œ μ•Œκ³  λ„˜μ–΄κ°€λŠ” μ •λ„λ‘œ μŠ΅λ“ν•˜μ‹œλ©΄ 될것 κ°™μŠ΅λ‹ˆλ‹€. 

객체의 속성

μžλ°”λ₯Ό 곡뢀해 보신뢄듀이라면 μ•„μ‹œκ² μ§€λ§Œ 객체 μ•ˆμ—λŠ” ν”„λ¦¬λ―Έν‹°λΈŒ λ©€λ²„λ³€μˆ˜λ‚˜ λ‹€λ₯Έ 객체λ₯Ό ν•˜λ‚˜μ˜ λ³€μˆ˜μ²˜λŸΌ 가지고 μžˆμŠ΅λ‹ˆλ‹€. 이런 λ©€λ²„λ³€μˆ˜λ“€μ„ μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” 속성이라고 μ–˜κΈ°λ₯Ό ν•©λ‹ˆλ‹€. 그리고 κ·Έ 속성듀에 값을 λ„£λŠ” 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.:
객체λͺ….속성λͺ… = 속성값;

예제:

μ•„λž˜ μ˜ˆμ œλŠ” 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