[Javascript / μλ°μ€ν¬λ¦½νΈ] κ°μ’ #16 - Object ( κ°μ²΄ )
μ΄λ² ν¬μ€νΈμμλ μλ°μ€ν¬λ¦½νΈμ κ°μ²΄μ λν΄μ μμλ³Όν λ°μ κ°μ²΄κ° 무μμΈμ§λ μκ³ κ³μ λ€λ μ μ νμ μ€λ¬΄ μμ£Όμ λ΄μ©μ μκΈ°ν΄λ³΄λλ‘ ν κ±°μμ. μλ°μ κ°μ²΄μ λμΌν κ²μ΄λΌκ³ μκ°νμλ©΄ λκ³ λ¨μ§ μ¬μ©λ²μ΄ μ΄λ€μ§μ λν΄μ μκ³ λμ΄κ°λ μ λλ‘ μ΅λνμλ©΄ λ κ² κ°μ΅λλ€.
κ°μ²΄μ μμ±
μλ°λ₯Ό 곡λΆν΄ 보μ λΆλ€μ΄λΌλ©΄ μμκ² μ§λ§ κ°μ²΄ μμλ ν리미ν°λΈ λ©€λ²λ³μλ λ€λ₯Έ κ°μ²΄λ₯Ό νλμ λ³μμ²λΌ κ°μ§κ³ μμ΅λλ€. μ΄λ° λ©€λ²λ³μλ€μ μλ°μ€ν¬λ¦½νΈμμλ μμ±μ΄λΌκ³ μκΈ°λ₯Ό ν©λλ€. κ·Έλ¦¬κ³ κ·Έ μμ±λ€μ κ°μ λ£λ λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.:κ°μ²΄λͺ .μμ±λͺ = μμ±κ°; |
μμ :
μλ μμ λ 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