ÀÚ¹Ù½ºÅ©¸³Æ® ÇÁ·ÎÅäŸÀÔÀ» »ç¿ëÇϸé ÇÁ·Î±×·¥ÀÇ È¿À²¼ºÀ» ³ôÈ÷°í ¸Þ¸ð¸®¸¦ Àý¾à ÇÒ¼ö ÀÖ´Ù. °´Ã¼ ÁöÇâ¾ð¾îÀÇ »ó¼Ó °³³äµµ Èä³» ³¾¼ö ÀÖ´Ù. prototypeÀÇ »ç¿ë ¹æ¹ýÀ» ¾Ë¾Æº¸ÀÚ. ÇÁ·ÎÅäŸÀÔÀ» »ç¿ëÇÑ ¸Þ¸ð¸® °øÀ¯var Person = function(){
this.eye = 3; } Person.prototype.setEye = function(eye) { this.eye = eye } Person.prototype.getEye = function() { console.log("Person " + this.eye); } var kim = new Person(); kim.setEye(2); var lee = new Person(); lee.setEye(1); °á°ú) kim 2 lee 1 ÇÁ·ÎÅäŸÀÔÀ» »ç¿ëÇϸé eye º¯¼ö°¡ °øÀ¯µÈ´Ù. var Person = function(){}
Person.prototype.eye = 3; Person.prototype.setEye = function(eye) { Person.prototype.eye = eye } Person.prototype.getEye = function() { console.log("Person " + this.eye); } var kim = new Person(); kim.setEye(2); var lee = new Person(); lee.setEye(1); console.log("kim " + kim.eye); console.log("lee " + lee.eye); °á°ú) kim 1 lee 1 ÇÁ·ÎÅäŸÀÔ °Ë»ö ¼ø¼¾Æ·¡ ¿¹Á¦´Â ÇÁ·ÎÅäŸÀÔÀ» »ç¿ëÇÏÁö ¾Ê°í ÀÖ´Ù.var Person = function(name){
this.name = name; this.getName = function(){ console.log(this.name); } } var kim = new Person('kim'); kim.getName(); var lee = new Person('lee'); lee.getName(); ´ÙÀ½Àº ÇÁ·ÎÅäŸÀÔÀ» »ç¿ëÇÏ°í ÀÖ´Ù. var Person = function(name){
this.name = name; this.getName = function(){ console.log("Person " + this.name); } } Person.prototype.getName = function(){ console.log("Prototype " + this.name); } var kim = new Person('kim'); kim.getName(); var lee = new Person('lee'); lee.getName(); °á°ú: Person kim Person lee ÇÁ·ÎÅäŸÀÔ¿¡¼ getNameÀ» ã´Â ¼ø¼´Â ´ÙÀ½°ú °°´Ù.
ÇÁ·ÎÅäŸÀÔÀ» ÀÌ¿ëÇÑ »ó¼ÓÇÁ·ÎÅäŸÀÔÀ» ÀÌ¿ëÇÏ¸é »ó¼Ó Èä³»¸¦ ³¾¼ö ÀÖ´Ù.1. ÀÚ¹Ù½ºÅ©¸³Æ® API-call ÇÔ¼ö·Î »ý¼ºÀÚ¿¡¼ ºÎ¸ð »ý¼ºÀÚ¸¦ È£Ãâ ÇÏ°í ÀÖ´Ù. Animal.call(this, name); 2. ºÎ¸ðÀÇ ÇÁ·ÎÅäŸÀÔ, ÇÁ·ÎÅäŸÀÔ »ý¼ºÀÚ¸¦ »ó¼Ó ¹Þ´Â´Ù. °´Ã¼¸¦ »ý¼ºÇϱâ ÀÌÀü¿¡ ÇØ¾ß ÇÑ´Ù. Monkey.prototype = Object.create(Animal.prototype); Monkey.prototype.constructor = Monkey; 3. ÇÔ¼ö¸¦ Àç Á¤ÀÇ ÇÑ´Ù. ÇÔ¼ö¸¦ Àç Á¤ÀÇÇÏ°í ³ª¼ ½ÇÇà µÇ´Â ÇÔ¼ö¿¡ ¿µÇâÀ» ÁØ´Ù. Monkey.prototype.walk = function() { console.log(this.name + "µÎ¹ß·Î °È´Â´Ù"); }; function Animal(name) {
this.name = name; }; Animal.prototype.walk = function() { console.log(this.name + "³×¹ß·Î °È´Â´Ù"); }; function Monkey(name) { Animal.call(this, name); } var dog = new Animal("dog"); //Monkey °´Ã¼¸¦ »ý¼ºÇÏ°í ³ª¼ protypÀ» ¼±¾îÇÏ¸é ´ÙÀ½ÀÇ ¿¡·¯°¡ ¹ß»ýÇÑ´Ù. //Uncaught TypeError: monkey.walk is not a function Monkey.prototype = Object.create(Animal.prototype); //= new Animal(); Monkey.prototype.constructor = Monkey; var monkey = new Monkey("monkey"); monkey.walk(); Monkey.prototype.walk = function() { console.log(this.name + "µÎ¹ß·Î °È´Â´Ù"); }; dog.walk(); monkey.walk(); console.dir(monkey); ½ÇÇà °á°ú: kim lee monkey³×¹ß·Î °È´Â´Ù dog³×¹ß·Î °È´Â´Ù monkeyµÎ¹ß·Î °È´Â´Ù Monkey °°Àº °´Ã¼¿¡¼ ´Ù¸¥ ÇÔ¼ö¸¦ »ç¿ëÇÏ´Â ¹æ¹ýµµ ÀÖ´Ù.È«±æµ¿Àº óÀ½¿¡´Â ³×¹ß·Î °É¾úÁö¸¸, µÎ¹ß·Î °È´Â ÇÔ¼ö¸¦ ¼±¾ðÇÏ°í ³ª¼´ÂµÎ¹ß·Î °È´Â´Ù. var human = new Animal("human");
human.walk(); human.myname = " È«±æµ¿ "; human.walk = function() { console.log(this.name + human.myname + " µåµð¾î µÎ¹ß·Î °È´Â´Ù"); }; human.walk(); ½ÇÇà °á°ú: human³×¹ß·Î °È´Â´Ù human È«±æµ¿ µåµð¾î µÎ¹ß·Î °È´Â´Ù ¼Ò½º: prototype.html Âü°í) https://bestalign.github.io/2015/08/02/JavaScript-Inheritance/ http://beomy.tistory.com/3 |