- 객체를 생성하고 객체를 돌려줌
- 객체를 생성하는 틀 역할
- new를 붙이면 새로운 객체를 생성한 후에 이를 리턴
- <script>
- function Make(name, top, bottom, color) {
- this.name = name;
- this.top = top;
- this.bottom = bottom;
- this.color = color;
- this.print = function() {
- var out='';
- out+='name:'+this.name+'\n';
- out+='top:'+this.top+'\n';
- out+='bottom:'+this.bottom+'\n';
- out+='color:'+this.color;
- console.log(out);
- }
- }
- var clothes = new Make('suit', 100, 32, 'black');
- console.log(clothes);
- </script>
① instanceof 키워드
(생성자 함수에 의해 생성된 함수가 어떤 생성자 함수로 인해 생성되었는지 확인)
- <script>
- function Make(name, top, bottom, color) {
- this.name = name;
- this.top = top;
- this.bottom = bottom;
- this.color = color;
- this.print = function() {
- var out='';
- out+='name:'+this.name+'\n';
- out+='top:'+this.top+'\n';
- out+='bottom:'+this.bottom+'\n';
- out+='color:'+this.color;
- console.log(out);
- }
- }
- var clothes = new Make('suit', 100, 32, 'black');
- console.log(clothes instanceof Make); // true
- </script>
댓글 없음:
댓글 쓰기