본문 바로가기

JavaScript31

JavaScript 기초(23)_객체(3) 1. 프로토타입의 상속 프로토타입의 상속에 대한 기초적인 예제를 아래와 같이 작성해보았다. // prototype 상속 function Person(){ } Person.prototype.hello = function(){ console.log('hello'); } function Korean(region){ this.region = region; this.where = function(){ console.log('where', this.region); }; } Korean.prototype = Person.prototype; const k = new Korean('seoul'); k.hello(); k.where(); [Running] n hello where seoul 비어있는 함수 Person의 프.. 2021. 10. 20.
JavaScript기초(22)_객체(2) 1. new Object() 우선 빈 객체를 만들어 그 결과 값과 타입을 출력하면 아래와 같이 나온다. const a = new Object(); console.log(a, typeof a); [Running] {} object Object에 true를 넣고 똑같이 출력해보자. const a = new Object(true); console.log(a, typeof a); [Running] [Boolean: true] object a는 불리언 true라는 결과 값과 타입은 객체라고 나온다. Object에 직접 리터럴 형태의 객체 값을 아래와 같이 넣으면 어떻게 될까? const a = new Object({name : 'Kim'}); console.log(a, typeof a); [Running] { n.. 2021. 10. 16.
JavaScript기초(21)_객체(1) 함수, 클래스 (틀) => 객체, 개체, object : 함수, 클래스 등의 틀을 이용해서 여러 객체(object)를 찍어내는 개념이다. 1. function 틀() {} => new 틀 () 비어있는 생성자 함수를 만들어보자. function A(){} // 텅빈 함수 A 생성함. const a = new A(); // 변수 a에 생성자함수 A를 선언함. = 객체 생성 console.log(a, typeof a); // 생성된 객체 a 와 a의 타입을 출력함. console.log(A()); // 함수 A() 자체를 출력함. [Running] A {} object undefined 객체 a를 출력하니 텅 빈 함수 'A {}'가 출력되었고, 'new A()'(생성자 함수)로 선언한 a의 타입은 'obje.. 2021. 10. 15.
JavaScript기초(20)_함수(6) 1. new 함수(); : 생성자 함수 생성자 함수의 기본적인 특징을 다음과 같은 코드를 통해서 살펴보자. function Person(name, age){ console.log(this); this.name = name; this.age = age; } const p = new Person('Mark', 37); const a = new Person('Anna', 31); console.log(p, p.name, p.age,); console.log(a, a.name, a.age); [Running] Person {} Person {} Person { name: 'Mark', age: 37 } Mark 37 Person { name: 'Anna', age: 31 } Anna 31 생성자 함수는 'thi.. 2021. 10. 14.