diff options
author | wikibook <dylee@wikibook.kr> | 2013-08-13 15:42:33 +0900 |
---|---|---|
committer | wikibook <dylee@wikibook.kr> | 2013-08-13 15:42:33 +0900 |
commit | 04c0b273b03b9417c895f687bcd67e7e58d71a38 (patch) | |
tree | 405a0226b497c71b1209ac0a6a7a1667d7f4c49f | |
parent | bd48b7062c51aff7d27114326f64fde505cea49c (diff) |
korean version of javascript and lua tutorials added
-rw-r--r-- | ko-kr/javascript-kr.html.markdown | 435 | ||||
-rw-r--r-- | ko-kr/lua-kr.html.markdown | 423 |
2 files changed, 858 insertions, 0 deletions
diff --git a/ko-kr/javascript-kr.html.markdown b/ko-kr/javascript-kr.html.markdown new file mode 100644 index 00000000..d9e0afe8 --- /dev/null +++ b/ko-kr/javascript-kr.html.markdown @@ -0,0 +1,435 @@ +--- +language: javascript +category: language +contributors: + - ["Adam Brenecki", "http://adam.brenecki.id.au"] +translators: + - ["wikibook", "http://wikibook.co.kr"] +lang: ko-kr +--- + +ÀÚ¹Ù½ºÅ©¸³Æ®´Â ³Ý½ºÄÉÀÌÇÁÀÇ ºê·»´ø ¾ÆÀÌÅ©(Brendan Eich)°¡ 1995³â¿¡ ¸¸µé¾ú½À´Ï´Ù. +¿ø·¡ ÀÚ¹Ù½ºÅ©¸³Æ®´Â À¥»çÀÌÆ®¸¦ À§ÇÑ ´Ü¼øÇÑ ½ºÅ©¸³Æ® ¾ð¾î¸¦ ¸ñÇ¥·Î ¸¸µé¾îÁ³´Âµ¥, +Á» ´õ º¹ÀâÇÑ À¥ ¾ÖÇø®ÄÉÀ̼ÇÀ» ¸¸µé±â À§ÇØ ÀÚ¹Ù¸¦ º¸¿ÏÇÏ´Â ¿ªÇÒÀ̾úÁö¸¸ +À¥ ÆäÀÌÁö¿ÍÀÇ ±ä¹ÐÇÑ »óÈ£ÀÛ¿ë°ú ºê¶ó¿ìÀú¿¡ ´ëÇÑ Áö¿ø ±â´É ´öºÐ¿¡ À¥ ÇÁ·ÐÆ®¿£µå¿¡¼ +ÀÚ¹Ùº¸´Ù ÈξÀ ´õ º¸ÆíÀûÀ¸·Î ¾²ÀÌ°Ô µÆ½À´Ï´Ù. + +±×·¸Áö¸¸ ÀÚ¹Ù½ºÅ©¸³Æ®´Â À¥ ºê¶ó¿ìÀú¿¡¸¸ ±¹ÇѵÇÁö ¾Ê½À´Ï´Ù. ±¸±Û Å©·ÒÀÇ V8 ÀÚ¹Ù½ºÅ©¸³Æ® +¿£ÁøÀ» À§ÇÑ µ¶¸³Çü ·±Å¸ÀÓÀ» Á¦°øÇÏ´Â Node.js´Â Á¡Á¡ Àα⸦ ¾ò°í ÀÖ½À´Ï´Ù. + +Çǵå¹é ÁÖ½Ã¸é ´ë´ÜÈ÷ °¨»çÇÏ°Ú½À´Ï´Ù! [@adambrenecki](https://twitter.com/adambrenecki)³ª +[adam@brenecki.id.au](mailto:adam@brenecki.id.au)¸¦ ÅëÇØ Àú¿Í ¸¸³ª½Ç ¼ö ÀÖ½À´Ï´Ù. + +```js +// ÁÖ¼®Àº C¿Í ºñ½ÁÇÕ´Ï´Ù. ÇÑ ÁÙÂ¥¸® ÁÖ¼®Àº µÎ °³ÀÇ ½½·¡½Ã·Î ½ÃÀÛÇÏ°í, +/* ¿©·¯ ÁÙ ÁÖ¼®Àº ½½·¡½Ã º°Ç¥·Î ½ÃÀÛÇؼ + º°Ç¥ ½½·¡½Ã·Î ³¡³³´Ï´Ù. */ + +// ±¸¹®Àº ¼¼¹ÌÄÝ·Ð(;)À¸·Î ³¡³¾ ¼ö ÀÖ½À´Ï´Ù. +doStuff(); + +// ÇÏÁö¸¸ ²À ±×·² ÇÊ¿ä´Â ¾ø´Âµ¥, ƯÁ¤ °æ¿ì¸¦ Á¦¿ÜÇÏ°í +// »õ ÁÙÀÌ ½ÃÀÛÇÒ ¶§¸¶´Ù ¼¼¹ÌÄÝ·ÐÀÌ ÀÚµ¿À¸·Î »ðÀԵDZ⠶§¹®ÀÔ´Ï´Ù. +doStuff() + +// ¿©±â¼´Â ¼¼¹ÌÄÝ·ÐÀ» »ý·«ÇÏ°Ú½À´Ï´Ù. ¼¼¹ÌÄÝ·ÐÀ» »ý·«ÇÒÁö ¿©ºÎ´Â +// °³ÀÎÀûÀÎ ÃëÇâÀ̳ª ÇÁ·ÎÁ§Æ®ÀÇ ½ºÅ¸ÀÏ °¡À̵带 µû¸¨´Ï´Ù. + +/////////////////////////////////// +// 1. ¼ýÀÚ, ¹®ÀÚ¿, ¿¬»êÀÚ + +// ÀÚ¹Ù½ºÅ©¸³Æ®¿¡´Â ´Ü ÇϳªÀÇ ¼ýÀÚ Å¸ÀÔ(64ºñÆ® IEEE 754 ¹èÁ¤µµ ¼ýÀÚ)¸¸ÀÌ +// ÀÖ½À´Ï´Ù. +3 // = 3 +1.5 // = 1.5 + +// ¸ðµç ±âÃÊ »ê¼ú ¿¬»êÀº ±â´ëÇÑ ´ë·Î µ¿ÀÛÇÕ´Ï´Ù. +1 + 1 // = 2 +8 - 1 // = 7 +10 * 2 // = 20 +35 / 5 // = 7 + +// ³ª´©¾î ¶³¾îÁöÁö ¾Ê´Â ³ª´°¼Àµµ Æ÷ÇԵ˴ϴÙ. +5 / 2 // = 2.5 + +// ºñÆ® ¿¬»êµµ Áö¿øµË´Ï´Ù. floatÀ» ´ë»óÀ¸·Î ºñÆ® ¿¬»êÀ» ¼öÇàÇϸé +// 32ºñÆ®±îÁö ºÎÈ£°¡ ÀÖ´Â int·Î º¯È¯µË´Ï´Ù. +1 << 2 // = 4 + +// °ýÈ£¸¦ ÀÌ¿ëÇÏ¸é ¿ì¼±¼øÀ§¸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù. +(1 + 3) * 2 // = 8 + +// ½ÇÁ¦ ¼ýÀÚ°¡ ¾Æ´Ñ Ưº°ÇÑ ¼¼ °¡Áö °ªÀÌ ÀÖ½À´Ï´Ù. +Infinity // 1/0 1/0°ú °°Àº ¿¬»êÀÇ °á°ú +-Infinity // -1/0°ú °°Àº ¿¬»êÀÇ °á°ú +NaN // 0/0°ú °°Àº ¿¬»êÀÇ °á°ú + +// ºÒ¸° ŸÀÔµµ ÀÖ½À´Ï´Ù. +true +false + +// ¹®ÀÚ¿Àº '³ª "·Î »ý¼ºÇÕ´Ï´Ù. +'abc' +"Hello, world" + +// ºÎÁ¤ ¿¬»ê¿¡´Â ! ±âÈ£¸¦ ÀÌ¿ëÇÕ´Ï´Ù. +!true // = false +!false // = true + +// µ¿Àϼº ¿¬»êÀº == +1 == 1 // = true +2 == 1 // = false + +// ºÒÀÏÄ¡ ¿¬»êÀº != +1 != 1 // = false +2 != 1 // = true + +// ±× ¹ÛÀÇ ºñ±³ ¿¬»ê +1 < 10 // = true +1 > 10 // = false +2 <= 2 // = true +2 >= 2 // = true + +// ¹®ÀÚ¿Àº +·Î ¿¬°áÇÒ ¼ö ÀÖ½À´Ï´Ù. +"Hello " + "world!" // = "Hello world!" + +// ±×¸®°í <¿Í >·Î ºñ±³ÇÒ ¼ö ÀÖ½À´Ï´Ù. +"a" < "b" // = true + +// ºñ±³ ½Ã ŸÀÔ °Á¦º¯È¯ÀÌ ¼öÇàµË´Ï´Ù. +"5" == 5 // = true + +// ===¸¦ ¾²Áö ¾Ê´Â´Ù¸é ¸»ÀÌÁÒ. +"5" === 5 // = false + +// charAtÀ» ÀÌ¿ëÇÏ¸é ¹®ÀÚ¿ ³»ÀÇ ¹®ÀÚ¿¡ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +"This is a string".charAt(0) + +// null°ú undefinedµµ ÀÖ½À´Ï´Ù. +null // ÀǵµÀûÀ¸·Î °ªÀÌ ¾Æ´ÔÀ» ³ªÅ¸³»´Â µ¥ »ç¿ëÇÕ´Ï´Ù. +undefined // °ªÀÌ ¾ÆÁ÷ ¼³Á¤µÇÁö ¾ÊÀ½À» ³ªÅ¸³»´Â µ¥ »ç¿ëÇÕ´Ï´Ù. + +// null, undefinded, NaN, 0, ""Àº °ÅÁþÀ̸ç, ±× ¹ÛÀÇ ´Ù¸¥ ¸ðµç °ªÀº ÂüÀÔ´Ï´Ù. +// Âü°í·Î 0Àº °ÅÁþÀ̸ç, "0"Àº ÂüÀÔ´Ï´Ù(½ÉÁö¾î 0 == "0"ÀÌ´õ¶óµµ). + +/////////////////////////////////// +// 2. º¯¼ö, ¹è¿, °´Ã¼ + +// º¯¼ö´Â var Å°¿öµå·Î ¼±¾ðÇÕ´Ï´Ù. ÀÚ¹Ù½ºÅ©¸³Æ®´Â µ¿Àû ŸÀÔ ¾ð¾î¶ó¼ +// ŸÀÔÀ» ÁöÁ¤ÇÒ ÇÊ¿ä°¡ ¾ø½À´Ï´Ù. °ªÀ» ÇÒ´çÇÒ ¶§´Â = ¹®ÀÚ Çϳª¸¦ »ç¿ëÇÕ´Ï´Ù. +var someVar = 5 + +// var Å°¿öµå¸¦ ÁöÁ¤ÇÏÁö ¾Ê¾Æµµ ¿À·ù´Â ¹ß»ýÇÏÁö ¾Ê½À´Ï´Ù. +someOtherVar = 10 + +// ±×·¸Áö¸¸ º¯¼ö°¡ ¿©·¯ºÐÀÌ Á¤ÀÇÇÑ À¯È¿¹üÀ§°¡ ¾Æ´Ï¶ó +// Àü¿ª À¯È¿¹üÀ§¿¡ »ý¼ºµË´Ï´Ù. + +// °ªÀ» ÇÒ´çÇÏÁö ¾ÊÀº ä·Î ¼±¾ðÇÑ º¯¼ö´Â undefined·Î ¼³Á¤µË´Ï´Ù. +var someThirdVar // = undefined + +// º¯¼ö¿¡ ¼öÇÐ ¿¬»êÀ» ¼öÇàÇÏ´Â Ãà¾àÇü Ç¥ÇöÀº ´ÙÀ½°ú °°½À´Ï´Ù. +someVar += 5 // someVar = someVar + 5;¿Í °°À½. ÀÌÁ¦ someVar´Â 10. +someVar *= 10 // somVar´Â 100 + +// 1À» ´õÇϰųª »©´Â ÈξÀ ´õ ªÀº Ç¥Çöµµ ÀÖ½À´Ï´Ù. +someVar++ // ÀÌÁ¦ someVar´Â 101 +someVar-- // ´Ù½Ã 100À¸·Î µÇµ¹¾Æ°¨ + +// ¹è¿Àº ¼øÂ÷ÀûÀÎ ÀÓÀÇ Å¸ÀÔ °ªÀÇ ¸ñ·ÏÀÔ´Ï´Ù. +var myArray = ["Hello", 45, true] + +// ¹è¿ÀÇ ¸â¹ö´Â ´ë°ýÈ£·Î µÑ·¯½Ñ À妽º¸¦ ÀÌ¿ëÇØ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +// ¹è¿ÀÇ À妽º´Â 0ºÎÅÍ ½ÃÀÛÇÕ´Ï´Ù. +myArray[1] // = 45 + +// ÀÚ¹Ù½ºÅ©¸³Æ®ÀÇ °´Ã¼´Â ´Ù¸¥ ¾ð¾îÀÇ '»çÀü'À̳ª '¸Ê'°ú °°½À´Ï´Ù. +// Áï, Å°-°ª ½ÖÀ¸·Î ±¸¼ºµÈ ºñ¼øÂ÷ Ä÷º¼ÇÀÔ´Ï´Ù. +{key1: "Hello", key2: "World"} + +// Å°´Â ¹®ÀÚ¿ÀÌÁö¸¸ À¯È¿ÇÑ ÀÚ¹Ù½ºÅ©¸³Æ® ½Äº°ÀÚÀÏ °æ¿ì +// ÀÛÀºµû¿ÈÇ¥´Â ÇÊ¿äÇÏÁö ¾Ê½À´Ï´Ù. °ªÀº ¾î¶² ŸÀÔÀÌµç »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. +var myObj = {myKey: "myValue", "my other key": 4} + +// °´Ã¼ ¼Ó¼º¿¡µµ À妽º¸¦ ÀÌ¿ëÇØ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +myObj["my other key"] // = 4 + +// ¶Ç´Â Å°°¡ À¯È¿ÇÑ ½Äº°ÀÚÀÏ °æ¿ì Á¡ Ç¥±â¹ýÀ» ÀÌ¿ëÇØ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +myObj.myKey // = "myValue" + +// °´Ã¼´Â º¯°æ °¡´ÉÇÕ´Ï´Ù. Áï, °ªÀ» º¯°æÇϰųª »õ Å°¸¦ Ãß°¡ÇÒ ¼ö ÀÖ½À´Ï´Ù. +myObj.myThirdKey = true + +// ¼³Á¤µÇÁö ¾ÊÀº °ª¿¡ Á¢±ÙÇÏ·Á°í Çϸé undefined°¡ ¹ÝȯµË´Ï´Ù. +myObj.myFourthKey // = undefined + +/////////////////////////////////// +// 3. ·ÎÁ÷°ú Á¦¾î ±¸Á¶ + +// if ±¸Á¶´Â ¿©·¯ºÐÀÌ ¿¹»óÇÑ ´ë·Î µ¿ÀÛÇÕ´Ï´Ù. +var count = 1 +if (count == 3){ + // count°¡ 3ÀÏ °æ¿ì Æò°¡µÊ +} else if (count == 4) { + // count°¡ 4ÀÏ °æ¿ì Æò°¡µÊ +} else { + // count°¡ 3À̳ª 4°¡ ¾Æ´Ñ °æ¿ì¿¡ Æò°¡µÊ +} + +// whileµµ ¸¶Âù°¡ÁöÀÔ´Ï´Ù. +while (true) { + // ¹«ÇÑ ·çÇÁ! +} + +// do-while ¹®Àº Ç×»ó ÃÖ¼Ò ÇÑ ¹øÀº ½ÇÇàµÈ´Ù´Â Á¡À» Á¦¿ÜÇϸé +// while ¹®°ú ºñ½ÁÇÕ´Ï´Ù. +var input +do { + input = getInput() +} while (!isValid(input)) + +// for ¹®Àº C¿Í ÀÚ¹ÙÀÇ for ¹®°ú °°½À´Ï´Ù. +// ÃʱâȽÄ; Áö¼Ó Á¶°Ç; Áõ°¨½Ä +for (var i = 0; i < 5; i++){ + // 5¹ø ½ÇÇàµÊ +} + +// &&´Â ³í¸® andÀÌ°í ||´Â ³í¸® orÀÔ´Ï´Ù. +if (house.size == "big" && house.colour == "blue"){ + house.contains = "bear" +} +if (colour == "red" || colour == "blue"){ + // »öÀº »¡°À̰ųª ÆĶû +} + +// &&¿Í ||Àº "´ÜÃà Æò°¡"¸¦ ¼öÇàÇϴµ¥, ±âº»°ªÀ» ¼³Á¤ÇÒ ¶§ À¯¿ëÇÕ´Ï´Ù. +var name = otherName || "default" + +/////////////////////////////////// +// 4. ÇÔ¼ö, À¯È¿¹üÀ§, Ŭ·ÎÀú + +// ÀÚ¹Ù½ºÅ©¸³Æ® ÇÔ¼ö´Â function Å°¿öµå·Î ¼±¾ðÇÕ´Ï´Ù. +function myFunction(thing){ + return thing.toUpperCase() +} +myFunction("foo") // = "FOO" + +// ÇÔ¼ö´Â "À͸í"À¸·Î, Áï À̸§ ¾øÀÌ Á¤ÀÇÇÒ ¼öµµ ÀÖ½À´Ï´Ù. +function(thing){ + return thing.toLowerCase() +} +// (ÇÔ¼ö¸¦ °¡¸®Å°´Â À̸§ÀÌ ¾ø±â ¶§¹®¿¡ ÇÔ¼ö¸¦ È£ÃâÇÒ ¼ö ¾ø½À´Ï´Ù) + +// ÀÚ¹Ù½ºÅ©¸³Æ® ÇÔ¼ö´Â ÀÏ±Þ °´Ã¼À̹ǷΠ´Ù¸¥ º¯¼ö¿¡ ÀçÇÒ´çÇÏ°í +// ´Ù¸¥ ÇÔ¼ö¿¡ ÀÎÀÚ·Î Àü´ÞÇÒ ¼ö ÀÖ½À´Ï´Ù. °¡·É, À̺¥Æ® Çڵ鷯¸¦ ¸¸µé °æ¿ì +function myFunction(){ + // ÀÌ ÄÚµå´Â 5ÃÊ ³»¿¡ È£ÃâµÊ +} +setTimeout(myFunction, 5000) + +// ´Ù¸¥ ÇÔ¼ö¸¦ È£ÃâÇÒ ¶§ Á÷Á¢ÀûÀ¸·Î ÇÔ¼ö ±¸¹®À» ÀÛ¼ºÇÒ ¼öµµ ÀÖ½À´Ï´Ù. + +setTimeout(function myFunction(){ + // ÀÌ ÄÚµå´Â 5ÃÊ ³»¿¡ È£ÃâµÊ +}, 5000) + +// ÀÚ¹Ù½ºÅ©¸³Æ®¿¡´Â ÇÔ¼ö À¯È¿¹üÀ§°¡ ÀÖ½À´Ï´Ù. +// ÇÔ¼ö´Â ÀÚüÀûÀÎ À¯È¿¹üÀ§¸¦ °¡ÁöÁö¸¸ ´Ù¸¥ ºí·ÏÀº À¯È¿¹üÀ§¸¦ °¡ÁöÁö ¾Ê½À´Ï´Ù. +if (true){ + var i = 5 +} +i // = 5 - ºí·Ï À¯È¿¹üÀ§¸¦ Áö¿øÇÏ´Â ¾ð¾î¿¡¼´Â undefined°¡ ¾Æ´Õ´Ï´Ù. + +// ÀÌ°ÍÀº "Áï½Ã ½ÇÇàµÇ´Â À͸í ÇÔ¼ö"¶ó´Â °øÅë ÆÐÅÏÀ¸·Î À̾îÁö´Âµ¥, +// ÀÌ ÆÐÅÏÀº Àӽà º¯¼ö°¡ Àü¿ª À¯È¿¹üÀ§·Î À¯ÃâµÇ´Â °ÍÀ» ¹æÁöÇÕ´Ï´Ù. +(function(){ + var temporary = 5 + // 'Àü¿ª °´Ã¼'¿¡ ÇÒ´çÇÏ´Â ½ÄÀ¸·Î Àü¿ª À¯È¿¹üÀ§¿¡ Á¢±ÙÇÒ ¼ö Àִµ¥, + // ºê¶ó¿ìÀú¿¡¼ Àü¿ª °´Ã¼´Â Ç×»ó 'window'ÀÔ´Ï´Ù. Àü¿ª °´Ã¼´Â + // Node.js¿Í °°Àº ºê¶ó¿ìÀú°¡ ¾Æ´Ñ ȯ°æ¿¡¼´Â ´Ù¸¥ À̸§ÀÏ ¼öµµ ÀÖ½À´Ï´Ù. + window.permanent = 10 + // ¶Ç´Â ¾Õ¿¡¼ ¾ð±ÞÇß´Ù½ÃÇÇ var Å°¿öµå¸¦ »¬ ¼öµµ ÀÖ½À´Ï´Ù. + permanent2 = 15 +})() +temporary // ReferenceError ¹ß»ý +permanent // = 10 +permanent2 // = 15 + +// ÀÚ¹Ù½ºÅ©¸³Æ®ÀÇ °·ÂÇÑ ±â´É Áß Çϳª´Â Ŭ·ÎÀú(closure)ÀÔ´Ï´Ù. +// ÇÔ¼ö°¡ ´Ù¸¥ ÇÔ¼ö ¾È¿¡¼ Á¤ÀÇµÇ¸é ¾ÈÂÊ¿¡ Á¤ÀÇµÈ ÇÔ¼ö´Â ¹Ù±ù ÇÔ¼öÀÇ +// ¸ðµç º¯¼ö¿¡ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +function sayHelloInFiveSeconds(name){ + var prompt = "Hello, " + name + "!" + function inner(){ + alert(prompt) + } + setTimeout(inner, 5000) + // setTimeoutÀº ºñµ¿±âÀûÀ¸·Î µ¿ÀÛÇϹǷΠÀÌ ÇÔ¼ö´Â 5ÃÊ µ¿¾È + // ±â´Ù¸®Áö ¾Ê°í ½ÇÇàÀ» ¸¶Ä¨´Ï´Ù. ÇÏÁö¸¸ 5ÃÊ°¡ Áö³ª¸é inner¿¡¼µµ + // promptÀÇ °ª¿¡ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +} +sayHelloInFiveSeconds("Adam") // will open a popup with "Hello, Adam!" in 5s + +/////////////////////////////////// +// 5. °´Ã¼ ½ÉÈ; »ý¼ºÀÚ¿Í ÇÁ·ÎÅäŸÀÔ + +// °´Ã¼´Â ÇÔ¼ö¸¦ Æ÷ÇÔÇÒ ¼ö ÀÖ½À´Ï´Ù. +var myObj = { + myFunc: function(){ + return "Hello world!" + } +} +myObj.myFunc() // = "Hello world!" + +// °´Ã¼¿¡ Æ÷ÇÔµÈ ÇÔ¼ö°¡ È£ÃâµÇ¸é ÇÔ¼ö¿¡¼´Â this Å°¿öµå¸¦ ÀÌ¿ëÇØ +// ÇØ´ç ÇÔ¼ö°¡ Æ÷ÇÔµÈ °´Ã¼¿¡ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +myObj = { + myString: "Hello world!", + myFunc: function(){ + return this.myString + } +} +myObj.myFunc() // = "Hello world!" + +// ¿©±â¼ ¼³Á¤ÇÑ °ÍÀº ÇÔ¼ö°¡ Á¤ÀÇµÈ °÷ÀÌ ¾Æ´Ñ ÇÔ¼ö°¡ È£ÃâµÇ´Â +// ¹æ½Ä°ú °ü·ÃÀÌ ÀÖ½À´Ï´Ù. ±×·¡¼ ¾Æ·¡ ÇÔ¼ö´Â °´Ã¼ ÄÁÅؽºÆ®¿¡¼ +// È£ÃâµÇÁö ¾ÊÀ¸¸é µ¿ÀÛÇÏÁö ¾Ê½À´Ï´Ù. +var myFunc = myObj.myFunc +myFunc() // = undefined + +// ¹Ý´ë·Î ÇÔ¼ö´Â °´Ã¼¿¡ ÇÒ´çÇÏ°í this¸¦ ÅëÇØ ÇØ´ç °´Ã¼¿¡ Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù. +// ÇÔ¼ö¸¦ Á¤ÀÇÇÒ ¶§ °´Ã¼¿¡ Ãß°¡µÇÁö ¾Ê¾Ò´õ¶óµµ ¸¶Âù°¡ÁöÀÔ´Ï´Ù. +var myOtherFunc = function(){ + return this.myString.toUpperCase() +} +myObj.myOtherFunc = myOtherFunc +myObj.myOtherFunc() // = "HELLO WORLD!" + +// new Å°¿öµå·Î ÇÔ¼ö¸¦ È£ÃâÇÏ¸é »õ·Î¿î °´Ã¼°¡ »ý¼ºµÇ°í this¸¦ ÅëÇØ +// ÇÔ¼ö¿¡¼ »ç¿ëÇÒ ¼ö ÀÖ°Ô µË´Ï´Ù. ÀÌ·± ½ÄÀ¸·Î ¼³°èµÈ ÇÔ¼ö¸¦ »ý¼ºÀÚ¶ó ÇÕ´Ï´Ù. + +var MyConstructor = function(){ + this.myNumber = 5 +} +myNewObj = new MyConstructor() // = {myNumber: 5} +myNewObj.myNumber // = 5 + +// ¸ðµç ÀÚ¹Ù½ºÅ©¸³Æ® °´Ã¼´Â 'prototype'À» °¡Áö°í ÀÖ½À´Ï´Ù. ¾î¶² °´Ã¼¿¡ ´ëÇØ +// ½ÇÁ¦ °´Ã¼¿¡´Â Á¸ÀçÇÏÁö ¾Ê´Â ÇÁ·ÎÆÛƼ¿¡ Á¢±ÙÇϸé ÀÎÅÍÇÁ¸®ÅÍ´Â ÇÁ·Î·ÎŸÀÔ¿¡¼ +// ÇØ´ç ÇÁ·ÎÆÛƼ¸¦ ã½À´Ï´Ù. + +// ÀϺΠÀÚ¹Ù½ºÅ©¸³Æ® ±¸Çöü¿¡¼´Â __proto__¶ó´Â ¸¶¹ýÀÇ ÇÁ·ÎÆÛƼ·Î +// °´Ã¼ÀÇ ÇÁ·ÎÅäŸÀÔ¿¡ Á¢±ÙÇÏ´Â °ÍÀ» Çã¿ëÇϱ⵵ ÇÕ´Ï´Ù. ÇÁ·ÎÅäŸÀÔÀ» +// ¼³¸íÇϱ⿡´Â ÀÌ·± ³»¿ëµµ µµ¿òµÇ°ÚÁö¸¸ __proto__´Â Ç¥ÁØ¿¡ Æ÷ÇԵŠ+// ÀÖÁö ¾Ê½À´Ï´Ù. ³ªÁß¿¡ ÇÁ·ÎÅäŸÀÔÀ» »ç¿ëÇϴ ǥÁØ ¹æ¹ýÀ» »ìÆ캸°Ú½À´Ï´Ù. +var myObj = { + myString: "Hello world!", +} +var myPrototype = { + meaningOfLife: 42, + myFunc: function(){ + return this.myString.toLowerCase() + } +} +myObj.__proto__ = myPrototype +myObj.meaningOfLife // = 42 + +// ÀÌ ¹æ¹ýÀº ÇÔ¼ö¿¡µµ ÅëÇÕ´Ï´Ù. +myObj.myFunc() // = "hello world!" + +// ¹°·Ð ÇÁ·ÎÆÛƼ°¡ ÇÁ·ÎÅäŸÀÔ¿¡ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é +// ÇÁ·ÎÅäŸÀÔÀÇ ÇÁ·ÎÅäŸÀÔÀ» ã´Â ½ÄÀ¸·Î ÁøÇàµË´Ï´Ù. +myPrototype.__proto__ = { + myBoolean: true +} +myObj.myBoolean // = true + +// ¿©±â¼ º¹»ç´Â ÀϾÁö ¾Ê½À´Ï´Ù. °¢ °´Ã¼¿¡´Â ÇÁ·ÎÅäŸÀÔ¿¡ ´ëÇÑ +// ÂüÁ¶°¡ º¸°üµÅ ÀÖ½À´Ï´Ù. ÀÌ´Â ÇÁ·ÎÅäŸÀÔÀ» º¯°æÇÏ¸é º¯°æ»çÇ×ÀÌ +// ¸ðµç °÷¿¡ ¹Ý¿µµÈ´Ù´Â ÀǹÌÀÔ´Ï´Ù. +myPrototype.meaningOfLife = 43 +myObj.meaningOfLife // = 43 + +// ¾Õ¿¡¼ __proto__°¡ Ç¥ÁØ¿¡ Æ÷ÇԵŠÀÖÁö ¾Ê´Ù°í À̾߱âÇߴµ¥, +// ±âÁ¸ °´Ã¼ÀÇ ÇÁ·ÎÅäŸÀÔÀ» º¯°æÇϴ ǥÁØ ¹æ¹ýÀº ¾ø½À´Ï´Ù. +// ÇÏÁö¸¸ ƯÁ¤ ÇÁ·ÎÅäŸÀÔÀ» °¡Áö°í »õ·Î¿î °´Ã¼¸¦ »ý¼ºÇÏ´Â µÎ °¡Áö +// ¹æ¹ýÀÌ ÀÖ½À´Ï´Ù. + +// ù ¹ø° ¹æ¹ýÀº Object.create¸¦ ÀÌ¿ëÇÏ´Â °ÍÀε¥, +// Object.create´Â ÃÖ±Ù¿¡ ÀÚ¹Ù½ºÅ©¸³Æ®¿¡ Ãß°¡µÈ °ÍÀÌ¶ó¼ ¾ÆÁ÷±îÁö +// ¸ðµç ±¸Çöü¿¡¼ ÀÌ¿ëÇÒ ¼ö ÀÖ´Â °ÍÀº ¾Æ´Õ´Ï´Ù. +var myObj = Object.create(myPrototype) +myObj.meaningOfLife // = 43 + +// µÎ ¹ø° ¹æ¹ýÀº ¾îµð¼³ª ÅëÇÏ´Â ¹æ¹ýÀε¥, »ý¼ºÀÚ¿Í °ü·ÃÀÌ ÀÖ½À´Ï´Ù. +// »ý¼ºÀÚ¿¡´Â prototypeÀ̶ó´Â ÇÁ·ÎÆÛƼ°¡ ÀÖ½À´Ï´Ù. ÀÌ ÇÁ·ÎÆÛƼ´Â +// »ý¼ºÀÚ ÇÔ¼ö ÀÚüÀÇ ÇÁ·ÎÅäŸÀÔÀÌ *¾Æ´Ï°í* »ý¼ºÀÚ¿Í new Å°¿öµå¸¦ ÀÌ¿ëÇØ +// °´Ã¼°¡ »ý¼ºµÉ ¶§ »õ·Î¿î °´Ã¼°¡ ¹Þ´Â ÇÁ·ÎÅäŸÀÔÀÔ´Ï´Ù. +myConstructor.prototype = { + getMyNumber: function(){ + return this.myNumber + } +} +var myNewObj2 = new myConstructor() +myNewObj2.getMyNumber() // = 5 + +// ¹®ÀÚ¿°ú ¼ýÀÚ¿Í °°Àº ³»Àå ŸÀÔ¿¡µµ µ¿µîÇÑ ·¡ÆÛ °´Ã¼¸¦ +// »ý¼ºÇÏ´Â »ý¼ºÀÚ°¡ ÀÖ½À´Ï´Ù. +var myNumber = 12 +var myNumberObj = new Number(12) +myNumber == myNumberObj // = true + +// ÇÏÁö¸¸ Á¤È®È÷ °°Áö´Â ¾Ê½À´Ï´Ù. +typeof(myNumber) // = 'number' +typeof(myNumberObj) // = 'object' +myNumber === myNumberObj // = false +if (0){ + // 0Àº °ÅÁþÀ̶ó¼ ÀÌ ÄÚµå´Â ½ÇÇàµÇÁö ¾Ê½À´Ï´Ù. +} +if (Number(0)){ + // Number(0)Àº ÂüÀ̶ó¼ ÀÌ ÄÚµå´Â *½ÇÇàµË´Ï´Ù*. +} + +// ÇÏÁö¸¸ ·¡ÆÛ °´Ã¼¿Í ÀÏ¹Ý ³»Àå ÇÔ¼ö´Â ÇÁ·ÎÅäŸÀÔÀ» °øÀ¯Çϱ⠶§¹®¿¡ +// °¡·É ¹®ÀÚ¿¿¡ ½ÇÁ¦·Î ±â´ÉÀ» Ãß°¡ÇÒ ¼ö ÀÖ½À´Ï´Ù. +String.prototype.firstCharacter = function(){ + return this.charAt(0) +} +"abc".firstCharacter() // = "a" + +// ÀÌ·¯ÇÑ »ç½ÇÀº ±âÁ¸ ÀÚ¹Ù½ºÅ©¸³Æ® ¹öÀü¿¡¼ ÀÚ¹Ù½ºÅ©¸³Æ®ÀÇ +// »õ·Î¿î ±â´ÉÀ» ±¸ÇöÇÏ´Â "Æú¸®ÇÊ(polyfilling)"¿¡ ÀÚÁÖ ÀÌ¿ëµÇ¹Ç·Î +// ¿À·¡µÈ ¹öÀüÀÇ ºê¶ó¿ìÀú¿Í °°ÀÌ ±âÁ¸ ȯ°æ¿¡¼ »ç¿ëµÉ ¼ö ÀÖ½À´Ï´Ù. + +// ¿¹¸¦ µé¾î, Object.create°¡ ¸ðµç ±¸Çöü¿¡¼ »ç¿ë °¡´ÉÇÑ °ÍÀº ¾Æ´Ï¶ó°í +// ÇßÁö¸¸ ¾Æ·¡ÀÇ Æú¸®ÇÊÀ» ÀÌ¿ëÇØ Object.create¸¦ ¿©ÀüÈ÷ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. +if (Object.create === undefined){ // don't overwrite it if it exists + Object.create = function(proto){ + // ¿Ã¹Ù¸¥ ÇÁ·ÎÅäŸÀÔÀ» °¡Áö°í Àӽà »ý¼ºÀÚ¸¦ ¸¸µë + var Constructor = function(){} + Constructor.prototype = proto + // ±×·± ´ÙÀ½ Àӽà »ý¼ºÀÚ¸¦ ÀÌ¿ëÇØ »õ·Î¿î ÀûÀýÇÑ ÇÁ·ÎÅäŸÀÔÀ» + // Æ÷ÇÔÇÑ °´Ã¼¸¦ »ý¼º + return new Constructor() + } +} +``` + +## ±âŸ Âü°í ÀÚ·á + +[¸ðÁú¶ó °³¹ßÀÚ ³×Æ®¿öÅ©](https://developer.mozilla.org/en-US/docs/Web/JavaScript)¿¡¼´Â +ÀÚ¹Ù½ºÅ©¸³Æ®¿¡ ´ëÇÑ ÈǸ¢ÇÑ ¹®¼¸¦ Á¦°øÇÕ´Ï´Ù. ´õºÒ¾î À§Å° Çü½ÄÀ̶ó¼ Á» ´õ ¸¹Àº »çÇ×À» +¹è¿ì°Ô µÇ¸é ¿©·¯ºÐ¸¸ÀÇ Áö½ÄÀ» °øÀ¯ÇÔÀ¸·Î½á ´Ù¸¥ »ç¶÷µé¿¡°Ô µµ¿òÀ» ÁÙ ¼öµµ ÀÖ½À´Ï´Ù. + +MDNÀÇ ['ÀÚ¹Ù½ºÅ©¸³Æ® ÀçÀÔ¹®'](https://developer.mozilla.org/ko/docs/A_re-introduction_to_JavaScript)¿¡¼´Â +¿©±â¼ ´Ù·é °³³äÀÇ »ó´ç¼ö¸¦ ´õ¿í ÀÚ¼¼È÷ ´Ù·ç°í ÀÖ½À´Ï´Ù. ÀÌ ÀÚ·á¿¡¼´Â ÀÚ¹Ù½ºÅ©¸³Æ® ¾ð¾î ÀÚü¿¡ +´ëÇؼ¸¸ »ó´çÈ÷ ½ÅÁßÇÏ°Ô ´Ù·ð½À´Ï´Ù. À¥ ÆäÀÌÁö¿¡¼ ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀ» ¹è¿ì°í ½Í´Ù¸é +[¹®¼ °´Ã¼ ¸ðµ¨(Document Object Model)](https://developer.mozilla.org/en-US/docs/Using_the_W3C_DOM_Level_1_Core)¿¡ +°üÇØ ¹è¿ì´Â °ÍÀ¸·Î ½ÃÀÛÇÏ±æ ¹Ù¶ø´Ï´Ù. + +[ÀÚ¹Ù½ºÅ©¸³Æ® °¡µç](http://bonsaiden.github.io/JavaScript-Garden/)¿¡¼´Â ÀÚ¹Ù½ºÅ©¸³Æ® ¾ð¾î¿¡¼ +Á÷°ü¿¡ ¾î±ß³ª´Â ¸ðµç ºÎºÐµéÀ» ½Éµµ ÀÖ°Ô ´Ù·ì´Ï´Ù. + +´õºÒ¾î ÀÌ ±Û¿¡ Á÷Á¢ÀûÀ¸·Î ±â¿©ÇÑ ºÐµé·Î, ³»¿ë Áß ÀϺδ ÀÌ »çÀÌÆ®¿¡ ÀÖ´Â +·çÀÌ µò(Louie Dihn)ÀÇ ÆÄÀ̽ã Æ©Å丮¾ó°ú ¸ðÁú¶ó °³¹ßÀÚ ³×Æ®¿öÅ©¿¡ ÀÖ´Â +[ÀÚ¹Ù½ºÅ©¸³Æ® Æ©Å丮¾ó](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript)À» Âü°íÇß½À´Ï´Ù.
\ No newline at end of file diff --git a/ko-kr/lua-kr.html.markdown b/ko-kr/lua-kr.html.markdown new file mode 100644 index 00000000..50458f88 --- /dev/null +++ b/ko-kr/lua-kr.html.markdown @@ -0,0 +1,423 @@ +--- +language: lua +category: language +contributors: + - ["Tyler Neylon", "http://tylerneylon.com/"] +translators: + - ["wikibook", "http://wikibook.co.kr"] +lang: ko-kr +--- + +```lua +-- ´ë½Ã µÎ °³´Â ÇÑ ÁÙÂ¥¸® ÁÖ¼®À» ÀǹÌÇÕ´Ï´Ù. + +--[[ + [¿Í ]¸¦ µÎ °³¾¿ Ãß°¡ÇÏ¸é ¿©·¯ ÁÙ ÁÖ¼®ÀÌ µË´Ï´Ù. +--]] + +---------------------------------------------------- +-- 1. º¯¼ö¿Í È帧 Á¦¾î +---------------------------------------------------- + +num = 42 -- ¸ðµç ¼ýÀÚ´Â doubleÀÔ´Ï´Ù. +-- ³î¶ö ÇÊ¿ä´Â ¾ø½À´Ï´Ù. 64ºñÆ® doubleÀº +-- Á¤È®ÇÑ int °ªÀ» ÀúÀåÇϱâ À§ÇØ 52ºñÆ®·Î ±¸¼ºµÅ +-- ÀÖ½À´Ï´Ù. 52ºñÆ® ÀÌÇÏÀÇ int °ª¿¡ ´ëÇؼ´Â +-- Àåºñ Á¤¹Ðµµ¿Í °ü·ÃµÈ ¹®Á¦°¡ »ý±âÁö ¾Ê½À´Ï´Ù. + +s = 'walternate' -- ÆÄÀ̽ã°ú °°Àº ºÒº¯ ¹®ÀÚ¿ +t = "Å«µû¿ÈÇ¥¸¦ ½áµµ µË´Ï´Ù" +u = [[ ÀÌÁß ´ë°ýÈ£´Â + ¿©·¯ ÁÙ ¹®ÀÚ¿À» + ³ªÅ¸³À´Ï´Ù.]] +t = nil -- ¹ÌÁ¤ÀÇ t. ·ç¾Æ´Â °¡ºñÁö Ä÷º¼ÇÀ» Áö¿øÇÕ´Ï´Ù. + +-- ºí·ÏÀº do/end¿Í °°Àº Å°¿öµå·Î ³ªÅ¸³À´Ï´Ù: +while num < 50 do + num = num + 1 -- ++³ª += À¯ÇüÀÇ ¿¬»êÀÚ´Â ¾µ ¼ö ¾ø½À´Ï´Ù. +end + +-- If Àý: +if num > 40 then + print('40 ÀÌ»ó') +elseif s ~= 'walternate' then -- ~=Àº '°°Áö ¾Ê´Ù'ÀÔ´Ï´Ù. + -- µ¿Àϼº °Ë»ç´Â ÆÄÀ̽ã°ú ¸¶Âù°¡Áö·Î ==ÀÔ´Ï´Ù. + -- ¹®ÀÚ¿¿¡µµ ¾µ ¼ö ÀÖ½À´Ï´Ù. + io.write('not over 40\n') -- ±âº»ÀûÀ¸·Î stdout¿¡ ¾¹´Ï´Ù. +else + -- º¯¼ö´Â ±âº»ÀûÀ¸·Î Àü¿ª º¯¼öÀÔ´Ï´Ù. + thisIsGlobal = 5 -- ³«Å¸ Ç¥±â¹ýÀÌ ÀϹÝÀûÀÔ´Ï´Ù. + + -- º¯¼ö¸¦ Áö¿ª º¯¼ö·Î ¸¸µå´Â ¹æ¹ýÀº ´ÙÀ½°ú °°½À´Ï´Ù: + local line = io.read() -- ´ÙÀ½ stdin ÁÙÀ» ÀнÀ´Ï´Ù + + -- ¹®ÀÚ¿ ¿¬°á¿¡´Â .. ¿¬»êÀÚ¸¦ ¾¹´Ï´Ù: + print('°Ü¿ïÀÌ ¿À°í ÀÖ½À´Ï´Ù, ' .. line) +end + +-- ¹ÌÁ¤ÀÇ º¯¼ö´Â nilÀ» ¹ÝȯÇÕ´Ï´Ù. +-- ´ÙÀ½ Äڵ带 ½ÇÇàÇصµ ¿À·ù°¡ ³ªÁö ¾Ê½À´Ï´Ù: +foo = anUnknownVariable -- ÀÌÁ¦ foo´Â nilÀÔ´Ï´Ù. + +aBoolValue = false + +-- nil°ú false¸¸ÀÌ °ÅÁþ°ªÀÔ´Ï´Ù; 0°ú ''Àº ÂüÀÔ´Ï´Ù! +if not aBoolValue then print('twas false') end + +-- 'or'¿Í 'and'´Â ´ÜÃà Æò°¡(short-circuit)µË´Ï´Ù. +-- ´ÙÀ½ ÄÚµå´Â C/ÀÚ¹Ù½ºÅ©¸³Æ®ÀÇ a?b:c ¿¬»êÀÚ¿Í ºñ½ÁÇÕ´Ï´Ù: +ans = aBoolValue and 'yes' or 'no' --> 'no' + +karlSum = 0 +for i = 1, 100 do -- ¹üÀ§¿¡´Â ¸¶Áö¸· ¿ä¼Òµµ Æ÷ÇԵ˴ϴÙ. + karlSum = karlSum + i +end + +-- Ä«¿îÆ® ´Ù¿îÀ» ÇÒ ¶§´Â "100, 1, -1"À» ¹üÀ§·Î ¾¹´Ï´Ù. +fredSum = 0 +for j = 100, 1, -1 do fredSum = fredSum + j end + +-- ÀϹÝÀûÀ¸·Î ¹üÀ§´Â begin, end[, step]ÀÔ´Ï´Ù. + +-- ¶Ç ´Ù¸¥ ¹Ýº¹¹® ±¸¹®Àº ´ÙÀ½°ú °°½À´Ï´Ù: +repeat + print('¹Ì·¡ÀÇ ¹æ½Ä') + num = num - 1 +until num == 0 + + +---------------------------------------------------- +-- 2. ÇÔ¼ö +---------------------------------------------------- + +function fib(n) + if n < 2 then return n end + return fib(n - 2) + fib(n - 1) +end + +-- Ŭ·ÎÀú¿Í À͸í ÇÔ¼öµµ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù: +function adder(x) + -- ¹ÝȯµÈ ÇÔ¼ö´Â adder°¡ È£ÃâµÉ ¶§ »ý¼ºµÇ°í xÀÇ + -- °ªÀÌ À¯ÁöµË´Ï´Ù: + return function (y) return x + y end +end +a1 = adder(9) +a2 = adder(36) +print(a1(16)) --> 25 +print(a2(64)) --> 100 + +-- ¹Ýȯ¹®, ÇÔ¼ö È£Ãâ, ÇÒ´ç¹®Àº ±æÀÌ°¡ ´Ù¸¥ +-- °ªÀÇ ¸®½ºÆ®¿¡ ´ëÇؼµµ ¸ðµÎ µ¿ÀÛÇÕ´Ï´Ù. +-- ¸®½ºÆ®¿¡ °ªÀÌ ´õ ÀûÀ» ¶§´Â nilÀÌ ÇÒ´ç/¹ÝȯµÇ°í +-- ¸®½ºÆ®¿¡ °ªÀÌ ´õ ¸¹À» ¶§´Â ³ª¸ÓÁö °ªÀº ¹ö·ÁÁý´Ï´Ù. + +x, y, z = 1, 2, 3, 4 +-- ÀÌÁ¦ x = 1, y = 2, z = 3ÀÌ°í 4´Â ¹ö·ÁÁý´Ï´Ù. + +function bar(a, b, c) + print(a, b, c) + return 4, 8, 15, 16, 23, 42 +end + +x, y = bar('zaphod') --> "zaphod nil nil"°¡ Ãâ·Â +-- ÀÌÁ¦ x = 4, y = 8ÀÌ°í 15~42ÀÇ °ªÀº ¹ö·ÁÁý´Ï´Ù. + +-- ÇÔ¼ö´Â ÀÏ±Þ °´Ã¼ÀÌ°í, Áö¿ª/Àü¿ª À¯È¿¹üÀ§¸¦ °¡Áú +-- ¼ö ÀÖ½À´Ï´Ù. ¾Æ·¡ÀÇ µÎ ÇÔ¼ö´Â °°½À´Ï´Ù: +function f(x) return x * x end +f = function (x) return x * x end + +-- ±×¸®°í ¾Æ·¡ÀÇ µÎ ÇÔ¼öµµ ¸¶Âù°¡ÁöÀÔ´Ï´Ù: +local function g(x) return math.sin(x) end +local g; g = function (x) return math.sin(x) end +-- 'local g'¶ó°í ¼±¾ðÇϸé g¸¦ Áö¿ª ÇÔ¼ö·Î ¸¸µì´Ï´Ù. + +-- ±×³ªÀú³ª »ï°¢ ÇÔ¼ö´Â ¶óµð¾È ´ÜÀ§·Î µ¿ÀÛÇÕ´Ï´Ù. + +-- ÇÔ¼ö¸¦ È£ÃâÇÒ ¶§ ¹®ÀÚ¿ ¸Å°³º¯¼ö¸¦ Çϳª¸¸ Àü´ÞÇÑ´Ù¸é +-- °ýÈ£¸¦ ¾²Áö ¾Ê¾Æµµ µË´Ï´Ù: +print 'hello' -- Àß µ¿ÀÛÇÕ´Ï´Ù. + + +---------------------------------------------------- +-- 3. Å×À̺í +---------------------------------------------------- + +-- Å×À̺í = ·ç¾ÆÀÇ À¯ÀÏÇÑ º¹ÇÕ ÀڷᱸÁ¶·Î¼, ¿¬°ü ¹è¿ÀÔ´Ï´Ù. +-- PHPÀÇ ¹è¿À̳ª ÀÚ¹Ù½ºÅ©¸³Æ®ÀÇ °´Ã¼¿Í ºñ½ÁÇϸç, +-- ¸®½ºÆ®·Îµµ »ç¿ëÇÒ ¼ö ÀÖ´Â Çؽà ±â¹ÝÀÇ µñ¼Å³Ê¸®ÀÔ´Ï´Ù. + +-- Å×À̺íÀ» µñ¼Å³Ê¸®/¸ÊÀ¸·Î »ç¿ëÇϱâ: + +-- µñ¼Å³Ê¸® ¸®ÅÍ·²Àº ±âº»ÀûÀ¸·Î ¹®ÀÚ¿ Å°¸¦ °¡Áý´Ï´Ù: +t = {key1 = 'value1', key2 = false} + +-- ¹®ÀÚ¿ Å°¿¡´Â ÀÚ¹Ù½ºÅ©¸³Æ®¿Í À¯»çÇÑ Á¡ Ç¥±â¹ýÀ» ¾µ ¼ö ÀÖ½À´Ï´Ù: +print(t.key1) -- 'value1'À» Ãâ·Â. +t.newKey = {} -- »õ Å°/°ª ½ÖÀ» Ãß°¡. +t.key2 = nil -- Å×ÀÌºí¿¡¼ key2¸¦ Á¦°Å. + +-- (nilÀÌ ¾Æ´Ñ) °ªÀ» Å°·Î »ç¿ëÇÏ´Â ¸®ÅÍ·² Ç¥±â¹ý: +u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'} +print(u[6.28]) -- "tau"°¡ Ãâ·Â + +-- Å° ¸ÅĪÀº ±âº»ÀûÀ¸·Î ¼ýÀÚ¿Í ¹®ÀÚ¿¿¡ ´ëÇؼ´Â °ªÀ¸·Î ÇÏÁö¸¸ +-- Å×ÀÌºí¿¡ ´ëÇؼ´Â ½Äº°ÀÚ·Î ÇÕ´Ï´Ù. +a = u['@!#'] -- Now a = 'qbert'. +b = u[{}] -- We might expect 1729, but it's nil: +a = u['@!#'] -- ÀÌÁ¦ a´Â 'qbert'ÀÔ´Ï´Ù. +b = u[{}] -- 1729¸¦ ¿¹»óÇß°ÚÁö¸¸ nilÀÔ´Ï´Ù: +-- Ž»öÀÌ ½ÇÆÐÇϱ⠶§¹®¿¡ b´Â nilÀÔ´Ï´Ù. Ž»öÀÌ ½ÇÆÐÇÏ´Â ÀÌÀ¯´Â +-- »ç¿ëµÈ Å°°¡ ¿øº» °ªÀ» ÀúÀåÇÒ ¶§ »ç¿ëÇÑ Å°¿Í µ¿ÀÏÇÑ °´Ã¼°¡ ¾Æ´Ï±â +-- ¶§¹®ÀÔ´Ï´Ù. µû¶ó¼ ¹®ÀÚ¿ ¹× ¼ýÀÚ°¡ Á» ´õ À̽ļº ÀÖ´Â Å°ÀÔ´Ï´Ù. + +-- Å×À̺í Çϳª¸¦ ¸Å°³º¯¼ö·Î ÃëÇÏ´Â ÇÔ¼ö¸¦ È£ÃâÇÒ ¶§´Â °ýÈ£°¡ ÇÊ¿äÇÏÁö ¾Ê½À´Ï´Ù: +function h(x) print(x.key1) end +h{key1 = 'Sonmi~451'} -- 'Sonmi~451'¸¦ Ãâ·Â. + +for key, val in pairs(u) do -- Å×ÀÌºí ¼øȸ + print(key, val) +end + +-- _G´Â ¸ðµç Àü¿ª ¸â¹ö¿¡ ´ëÇÑ Æ¯º°ÇÑ Å×À̺íÀÔ´Ï´Ù. +print(_G['_G'] == _G) -- 'true'°¡ Ãâ·Â + +-- Å×À̺íÀ» ¸®½ºÆ®/¹è¿·Î »ç¿ëÇϱâ: + +-- ¸®½ºÆ® ¸®ÅÍ·²Àº ¾Ï¹¬ÀûÀ¸·Î int Å°·Î ¼³Á¤µË´Ï´Ù: +v = {'value1', 'value2', 1.21, 'gigawatts'} +for i = 1, #v do -- #v´Â ¸®½ºÆ® vÀÇ Å©±âÀÔ´Ï´Ù. + print(v[i]) -- À妽º°¡ 1¿¡¼ ½ÃÀÛÇÕ´Ï´Ù!! Á¦Á¤½ÅÀÌ ¾Æ´Õ´Ï´Ù! +end +-- 'list'´Â ½ÇÁ¦ ŸÀÔÀÌ ¾Æ´Õ´Ï´Ù. v´Â ¿¬¼ÓµÈ Á¤¼öÇü Å°°¡ Æ÷ÇÔµÈ +-- Å×À̺íÀÌ°í ¸®½ºÆ®·Î Ãë±ÞµÉ »ÓÀÔ´Ï´Ù. + +---------------------------------------------------- +-- 3.1 ¸ÞŸÅ×À̺í°ú ¸ÞŸ¸Þ¼µå +---------------------------------------------------- + +-- Å×À̺íÀº Å×ÀÌºí¿¡ ¿¬»êÀÚ ¿À¹ö·ÎµùÀ» °¡´ÉÇÏ°Ô ÇÏ´Â ¸ÞŸÅ×À̺íÀ» +-- °¡Áú ¼ö ÀÖ½À´Ï´Ù. ³ªÁß¿¡ ¸ÞŸÅ×À̺íÀÌ ¾î¶»°Ô ÀÚ¹Ù½ºÅ©¸³Æ® +-- ÇÁ·ÎÅäŸÀÔ°ú °°Àº ÇàÀ§¸¦ Áö¿øÇÏ´ÂÁö »ìÆ캸°Ú½À´Ï´Ù. + +f1 = {a = 1, b = 2} -- ºÐ¼ö a/b¸¦ Ç¥Çö +f2 = {a = 2, b = 3} + +-- ´ÙÀ½ ÄÚµå´Â ½ÇÆÐÇÕ´Ï´Ù: +-- s = f1 + f2 + +metafraction = {} +function metafraction.__add(f1, f2) + sum = {} + sum.b = f1.b * f2.b + sum.a = f1.a * f2.b + f2.a * f1.b + return sum +end + +setmetatable(f1, metafraction) +setmetatable(f2, metafraction) + +s = f1 + f2 -- f1ÀÇ ¸ÞŸÅ×À̺íÀ» ´ë»óÀ¸·Î __add(f1, f2)¸¦ È£Ãâ + +-- f1°ú f2´Â ÀÚ¹Ù½ºÅ©¸³Æ®ÀÇ ÇÁ·ÎÅäŸÀÔ°ú ´Þ¸® °¢ ¸ÞŸÅ×ÀÌºí¿¡ ´ëÇÑ +-- Å°°¡ ¾ø¾î¼ getmetatable(f1)°ú °°ÀÌ ¹Þ¾Æ¿Í¾ß ÇÕ´Ï´Ù. +-- ¸ÞŸÅ×À̺íÀº __add °°Àº ·ç¾Æ°¡ ¾Ë°í ÀÖ´Â Å°°¡ ÁöÁ¤µÈ ÀÏ¹Ý Å×À̺íÀÔ´Ï´Ù. + +-- ±×·¸Áö¸¸ ´ÙÀ½ ÁÙÀº s°¡ ¸ÞŸÅ×À̺íÀ» °¡Áö°í ÀÖÁö ¾Ê±â ¶§¹®¿¡ ½ÇÆÐÇÕ´Ï´Ù. +-- t = s + s +-- ¾Æ·¡¿Í °°ÀÌ Å¬·¡½º¿Í À¯»çÇÑ ÆÐÅÏÀº ÀÌ·¯ÇÑ ¹®Á¦°¡ ¹ß»ýÇÏÁö ¾Ê½À´Ï´Ù. + +-- ¸ÞŸÅ×ÀÌºí¿¡ ´ëÇÑ __index´Â Á¡À» ÀÌ¿ëÇÑ Å½»öÀ» ¿À¹ö·ÎµåÇÕ´Ï´Ù: +defaultFavs = {animal = 'gru', food = 'donuts'} +myFavs = {food = 'pizza'} +setmetatable(myFavs, {__index = defaultFavs}) +eatenBy = myFavs.animal -- µ¿ÀÛÇÕ´Ï´Ù! °í¸¶¿ö¿ä, ¸ÞŸÅ×À̺í! + +-- Á÷Á¢ÀûÀÎ ¸ÞŸÅ×À̺í Ž»öÀÌ ½ÇÆÐÇÒ °æ¿ì ¸ÞŸÅ×À̺íÀÇ __index °ªÀ» ÀÌ¿ëÇØ +-- Àç½ÃµµÇÏ°í, ÀÌ·± °úÁ¤ÀÌ ¹Ýº¹µË´Ï´Ù. + +-- __index °ªÀº Á» ´õ ¼¼ºÐÈµÈ Å½»öÀ» À§ÇØ function(tbl, key)°¡ +-- µÉ ¼öµµ ÀÖ½À´Ï´Ù. + +-- __index, __add, ...ÀÇ °ªÀ» ¸ÞŸ¸Þ¼µå¶ó°í ÇÕ´Ï´Ù. +-- ´ÙÀ½Àº ¸ÞŸ¸Þ¼µå¸¦ °¡Áø Å×À̺íÀÇ Àüü ¸ñ·ÏÀÔ´Ï´Ù. + +-- __add(a, b) for a + b +-- __sub(a, b) for a - b +-- __mul(a, b) for a * b +-- __div(a, b) for a / b +-- __mod(a, b) for a % b +-- __pow(a, b) for a ^ b +-- __unm(a) for -a +-- __concat(a, b) for a .. b +-- __len(a) for #a +-- __eq(a, b) for a == b +-- __lt(a, b) for a < b +-- __le(a, b) for a <= b +-- __index(a, b) <fnÀ̳ª Å×À̺í> for a.b +-- __newindex(a, b, c) for a.b = c +-- __call(a, ...) for a(...) + +---------------------------------------------------- +-- 3.2 Ŭ·¡½º ÇüÅÂÀÇ Å×À̺í°ú »ó¼Ó +---------------------------------------------------- + +-- ·ç¾Æ¿¡´Â Ŭ·¡½º°¡ ³»ÀåµÅ ÀÖÁö ¾ÊÀ¸¸ç, Å×À̺í°ú ¸ÞŸÅ×À̺íÀ» +-- ÀÌ¿ëÇØ Å¬·¡½º¸¦ ¸¸µå´Â ´Ù¾çÇÑ ¹æ¹ýÀÌ ÀÖ½À´Ï´Ù. + +-- ´ÙÀ½ ¿¹Á¦¿¡ ´ëÇÑ ¼³¸íÀº ÇÏ´ÜÀ» ÂüÁ¶ÇÕ´Ï´Ù. + +Dog = {} -- 1. + +function Dog:new() -- 2. + newObj = {sound = 'woof'} -- 3. + self.__index = self -- 4. + return setmetatable(newObj, self) -- 5. +end + +function Dog:makeSound() -- 6. + print('I say ' .. self.sound) +end + +mrDog = Dog:new() -- 7. +mrDog:makeSound() -- 'I say woof' -- 8. + +-- 1. Dog´Â Ŭ·¡½ºÃ³·³ µ¿ÀÛÇÕ´Ï´Ù. ½ÇÁ¦·Î´Â Å×À̺íÀÔ´Ï´Ù. +-- 2. function Å×À̺í¸í:fn(...)Àº +-- function Å×À̺í¸í.fn(self, ...)°ú °°½À´Ï´Ù. +-- :´Â self¶ó´Â ù ¹ø° ÀÎÀÚ¸¦ Ãß°¡ÇÒ »ÓÀÔ´Ï´Ù. +-- self°¡ °ªÀ» ¾î¶»°Ô ¾ò´ÂÁö ±Ã±ÝÇÏ´Ù¸é ¾Æ·¡ÀÇ 7°ú 8À» Àо¼¼¿ä. +-- 3. newObj´Â Dog Ŭ·¡½ºÀÇ ÀνºÅϽº°¡ µË´Ï´Ù. +-- 4. self = ÀνºÅϽºÈµÇ´Â Ŭ·¡½º. +-- ÁÖ·Î self = DogÀÌÁö¸¸ »ó¼ÓÀ» ÀÌ¿ëÇϸé ÀÌ°ÍÀ» ¹Ù²Ü ¼ö ÀÖ½À´Ï´Ù. +-- newObjÀÇ ¸ÞŸÅ×À̺í°ú selfÀÇ __index¸¦ ¸ðµÎ self¿¡ ¼³Á¤Çϸé +-- newObj°¡ selfÀÇ ÇÔ¼ö¸¦ °®°Ô µË´Ï´Ù. +-- 5. Âü°í: setmetatableÀº ù ¹ø° ÀÎÀÚ¸¦ ¹ÝȯÇÕ´Ï´Ù. +-- 6. :´Â 2¿¡¼ ¼³¸íÇÑ °Í°ú °°ÀÌ µ¿ÀÛÇÏÁö¸¸ À̹ø¿¡´Â self°¡ +-- Ŭ·¡½º°¡ ¾Æ´Ñ ÀνºÅϽº¶ó°í ¿¹»óÇÒ ¼ö ÀÖ½À´Ï´Ù. +-- 7. Dog.new(Dog)°ú °°À¸¹Ç·Î new()¿¡¼´Â self = DogÀÔ´Ï´Ù. +-- 8. mrDog.makeSound(mrDog)°ú °°À¸¹Ç·Î self = mrDogÀÔ´Ï´Ù. + +---------------------------------------------------- + +-- »ó¼Ó ¿¹Á¦: + +LoudDog = Dog:new() -- 1. + +function LoudDog:makeSound() + s = self.sound .. ' ' -- 2. + print(s .. s .. s) +end + +seymour = LoudDog:new() -- 3. +seymour:makeSound() -- 'woof woof woof' -- 4. + +-- 1. LoudDogÀº DogÀÇ ¸Þ¼µå¿Í º¯¼ö¸¦ °®°Ô µË´Ï´Ù. +-- 2. self´Â new()¿¡¼ 'sound' Å°¸¦ °¡Áý´Ï´Ù. 3À» Âü°íÇϼ¼¿ä. +-- 3. LoudDog.new(LoudDog)°ú °°°í, LoudDogÀº 'new' Å°°¡ ¾øÁö¸¸ +-- ¸ÞŸÅ×ÀÌºí¿¡¼ __index = DogÀ̱⠶§¹®¿¡ Dog.new(LoudDog)À¸·Î +-- º¯È¯µË´Ï´Ù. +-- °á°ú: seymourÀÇ ¸ÞŸÅ×À̺íÀº LoudDogÀÌ°í LoudDog.__index´Â +-- LoudDogÀÔ´Ï´Ù. µû¶ó¼ seymour.key´Â seymour.key, +-- LoudDog.key, Dog.key¿Í °°À» °ÍÀ̸ç, ÁöÁ¤ÇÑ Å°¿¡ ¾î¶² Å×À̺íÀÌ +-- ¿Àµç »ó°ü¾øÀ» °ÍÀÔ´Ï´Ù. +-- 4. 'makeSound' Å°´Â LoudDog¿¡¼ ¹ß°ßÇÒ ¼ö ÀÖ½À´Ï´Ù. +-- ÀÌ°ÍÀº LoudDog.makeSound(seymour)¿Í °°½À´Ï´Ù. + +-- ÇÊ¿äÇÒ °æ¿ì, ÇÏÀ§ Ŭ·¡½ºÀÇ new()´Â ±â¹Ý Ŭ·¡½ºÀÇ new()¿Í À¯»çÇÕ´Ï´Ù. +function LoudDog:new() + newObj = {} + -- set up newObj + self.__index = self + return setmetatable(newObj, self) +end + +---------------------------------------------------- +-- 4. ¸ðµâ +---------------------------------------------------- + + +--[[ ¿©±â¼ ÁÖ¼®À» Á¦°ÅÇϸé ÀÌ ½ºÅ©¸³Æ®ÀÇ ³ª¸ÓÁö ºÎºÐÀº +-- ½ÇÇà °¡´ÉÇÑ »óÅ°¡ µË´Ï´Ù. +``` + +```lua +-- mod.lua ÆÄÀÏÀÇ ³»¿ëÀÌ ´ÙÀ½°ú °°´Ù°í °¡Á¤ÇØ º¾½Ã´Ù. +local M = {} + +local function sayMyName() + print('À̼ҷæ') +end + +function M.sayHello() + print('¾È³çÇϼ¼¿ä') + sayMyName() +end + +return M + +-- ¶Ç ´Ù¸¥ ÆÄÀÏ¿¡¼´Â mod.luaÀÇ ±â´ÉÀ» ÀÌ¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. +local mod = require('mod') -- mod.lua ÆÄÀÏÀ» ½ÇÇà + +-- require´Â ¸ðµâÀ» Æ÷ÇÔ½ÃÅ°´Â Ç¥ÁØÈµÈ ¹æ¹ýÀÔ´Ï´Ù. +-- require´Â ´ÙÀ½°ú °°ÀÌ µ¿ÀÛÇÕ´Ï´Ù: (ij½ÌµÅ ÀÖÁö ¾ÊÀ» °æ¿ì. ÇÏ´Ü ÂüÁ¶) +local mod = (function () + <mod.luaÀÇ ³»¿ë> +end)() +-- mod.lua°¡ ÇÔ¼öÀÇ º»¹®Ã³·³ µÇ¹Ç·Î mod.lua ¾ÈÀÇ Áö¿ª ¸â¹ö´Â +-- ¹Û¿¡¼ º¼ ¼ö ¾ø½À´Ï´Ù. + +-- ´ÙÀ½ Äڵ尡 µ¿ÀÛÇÏ´Â °ÍÀº mod°¡ mod.luaÀÇ M°ú °°±â ¶§¹®ÀÔ´Ï´Ù. +mod.sayHello() -- ÀÌ¼Ò·æ ¾¾¿¡°Ô Àλ縦 °Ç³Ü´Ï´Ù. + +-- ´ÙÀ½ Äڵ带 ½ÇÇàÇÏ¸é ¿À·ù°¡ ¹ß»ýÇÕ´Ï´Ù. +-- sayMyName´Â mod.lua ¾È¿¡¼¸¸ Á¸ÀçÇϱ⠶§¹®ÀÔ´Ï´Ù: +mod.sayMyName() -- ¿À·ù + +-- requireÀÇ ¹Ýȯ°ªÀº ij½ÌµÇ¹Ç·Î require¸¦ ¿©·¯ ¹ø ½ÇÇàÇصµ +-- ÆÄÀÏÀº ÃÖ´ë ÇÑ ¹ø¸¸ ½ÇÇàµË´Ï´Ù. + +-- mod2.lua¿¡ "print('Hi')"°¡ µé¾î ÀÖ´Ù°í °¡Á¤ÇØ º¾½Ã´Ù. +local a = require('mod2') -- Hi!¸¦ Ãâ·Â +local b = require('mod2') -- print¸¦ ½ÇÇàÇÏÁö ¾ÊÀ½. a=b + +-- dofileÀº require¿Í ºñ½ÁÇÏÁö¸¸ ij½ÌÀ» ÇÏÁö ¾Ê½À´Ï´Ù: +dofile('mod2') --> Hi! +dofile('mod2') --> Hi! (require¿Í ´Þ¸® ´Ù½Ã Çѹø ½ÇÇàµÊ) + +-- loadfileÀº ·ç¾Æ ÆÄÀÏÀ» ÀоîµéÀÌÁö¸¸ ½ÇÇàÇÏÁö´Â ¾Ê½À´Ï´Ù +f = loadfile('mod2') -- f()¸¦ È£ÃâÇØ¾ß mod2.lua°¡ ½ÇÇàµË´Ï´Ù. + +-- loadstringÀº ¹®ÀÚ¿¿¡ ´ëÇÑ loadfileÀÔ´Ï´Ù. +g = loadstring('print(343)') -- ÇÔ¼ö¸¦ ¹ÝȯÇÕ´Ï´Ù. +g() -- 343ÀÌ Ãâ·ÂµË´Ï´Ù. ±×Àü±îÁö´Â ¾Æ¹«°Íµµ Ãâ·ÂµÇÁö ¾Ê½À´Ï´Ù. + +--]] + +``` +## Âü°íÀÚ·á + +·ç¾Æ¸¦ ¹è¿ì´Â ÀÏÀÌ Èï¹ÌÁøÁøÇß´ø ÀÌÀ¯´Â <a href="http://love2d.org/">Love 2D °ÔÀÓ ¿£Áø</a>À» ÀÌ¿ëÇØ +°ÔÀÓÀ» ¸¸µé ¼ö ÀÖ¾ú±â ¶§¹®ÀÔ´Ï´Ù. ÀÌ°ÍÀÌ Á¦°¡ ·ç¾Æ¸¦ ¹è¿î ÀÌÀ¯ÀÔ´Ï´Ù. + +Àú´Â <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIVÀÇ "ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ ·ç¾Æ"</a>·Î +½ÃÀÛÇß½À´Ï´Ù. ±×´ÙÀ½À¸·Î °ø½Ä <a href="http://www.lua.org/pil/contents.html">"ÇÁ·Î±×·¡¹Ö ·ç¾Æ"</a> Ã¥À» Àоú½À´Ï´Ù. +±×·¸°Ô ·ç¾Æ¸¦ ¹è¿ü½À´Ï´Ù. + +lua-users.org¿¡ ÀÖ´Â <a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">ªÀº ·ç¾Æ ·¹ÆÛ·±½º</a>¸¦ +ÀоîµÎ¸é µµ¿òµÉÁöµµ ¸ð¸£°Ú½À´Ï´Ù. + +¿©±â¼´Â Ç¥ÁØ ¶óÀ̺귯¸®¿¡ °üÇؼ´Â ´Ù·çÁö ¾Ê¾Ò½À´Ï´Ù. + +* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string ¶óÀ̺귯¸®</a> +* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table ¶óÀ̺귯¸®</a> +* <a href="http://lua-users.org/wiki/MathLibraryTutorial">math ¶óÀ̺귯¸®</a> +* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io ¶óÀ̺귯¸®</a> +* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os ¶óÀ̺귯¸®</a> + +±×³ªÀú³ª ÀÌ ÆÄÀÏ Àüü´Â À¯È¿ÇÑ ·ç¾Æ ÇÁ·Î±×·¥ÀÔ´Ï´Ù. ÀÌ ÆÄÀÏÀ» +learn.lua·Î ÀúÀåÇÑ ÈÄ "lua learn.lua"¸¦ ½ÇÇàÇØ º¸¼¼¿ä! + +ÀÌ ±ÛÀº tylerneylon.com¿¡ óÀ½À¸·Î ½áº» ±ÛÀ̸ç, +<a href="https://gist.github.com/tylerneylon/5853042">GithubÀÇ Gist</a>¿¡¼µµ È®ÀÎÇÒ ¼ö ÀÖ½À´Ï´Ù. +·ç¾Æ·Î Áñ°Å¿î ½Ã°£À» º¸³»¼¼¿ä!
\ No newline at end of file |