1 2 3 4 5 6 7 8 9 10 11 12 13 | function Wrap(name,role){ this.name = name; this.role = role; this.say = function(){ console.log(this.name + "은" + this.role + "을(를) 맡고 있습니다."); }; } const hong = new Wrap("홍길동","퍼블리셔"); //프로토타입 : Wrap.prototype const sung = new Wrap("성춘향","디자이너"); //프로토타입 : Wrap.prototype hong.say(); //출력결과 : 홍길동은 퍼블리셔을(를) 맡고 있습니다. sung.say(); //출력결과 : 성춘향은 디자이너을(를) 맡고 있습니다. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function Wrap(name,role){ this.name = name; this.role = role; this.say = function(){ console.log(this.name + "은" + this.role + "을(를) 맡고 있습니다."); }; } const hong = new Wrap("홍길동","퍼블리셔"); const sung = new Wrap("성춘향","디자이너"); console.log(hong.hasOwnProperty('name'); //출력결과 : true; console.log(hong.hasOwnproperty('hi'); //출력결과 : false; console.log(hong.abcdefg()); //출력결과 : Uncaught TypeError : hong.abcdefg is not a function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function Person(name,age,height,job){ this.name = name; this.age = age; this.height = height; this.job = job; } Person.prototype.info = function(){ console.log(this.name + '의 나이는 ' + this.age + '세, 키는 ' + this.height + 'cm 이며 직업은' + this.job + '입니다.'); } const kimwoobin = new Person("김우빈",34,188,"배우"); const joinsung = new Person("조인성",40,186,"배우"); kimwoobin.info(); //출력결과 : 김우빈의 나이는 34세, 키는 188cm 이며 직업은 배우입니다. joinsung.info(); //출력결과 : 조인성의 나이는 40세, 키는 186cm 이며 직업은 배우입니다. |
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Person(name){ this.name = name; }; Person.prototype.info = function(){ return this.name; } const kimwoobin = new Person("김우빈"); cosnole.log(kimwoobin.info()); //출력결과 : 김우빈 Person.prototype.name = "홍길동"; console.log(Person.prototype.info()); //출력결과 : 홍길동 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function Hello(name){ this.name = name; } console.log(Hello.prototype.constructor); //출력결과 : Hello(name); const abc = new Hello("홍길동"); console.log(abc.name); //출력결과 : 홍길동; Hello.prototype = { 이름:'홍길동', 나라:'대한민국' } console.log(Hello.prototype.constructor); //출력결과 : Object(); (왜냐면 리터럴 방식으로 변경했기 때문에 object가 프로토타입이 된다. const h1 = new Hello(); console.log(hi.나라); //출력결과 : 대한민국 |
1 2 3 4 5 6 7 8 9 10 11 12 | function Hello(old){ this.old = old; } Hello.prototype.name = "홍길동"; const a = new Hello(24); console.log(a.old); //출력결과 : 24 console.log(a.name); //출력결과 : 홍길동 a.name = "김수한무"; //a 객체 안에 name 이라는 프로퍼티를 생성한다. console.log(a.name); //출력결과 : 김수한무 (a 내부에 name 이라는 프로퍼티가 생겼기 때문에 prototype까지 올라가지 않았다.) |
더 편해진 도구와 새로운 콘텐츠로 찾아올게요.
지금 쓰시는 도구 주소는 그대로예요.