-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript12.html
More file actions
67 lines (56 loc) · 2.63 KB
/
JavaScript12.html
File metadata and controls
67 lines (56 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<html lang="ko">
<head>
<title>객체 관련 키워드</title>
<style>
.area{
background: lightgray;
border: 1px solid black;
height: 100px;
}
</style>
</head>
<body>
<h1>객체 관련 키워드</h1>
<h3>instanceof 키워드</h3>
<p>해당 객체가 어떠한 생성자 함수를 통해 생성됐는지 확인할 때 사용한다.</p>
<button onclick="test1();">instanceof 확인하기</button>
<div id="area1" class="area"></div>
<script>
//생성자 함수
function Dog(name){this.name = name};
//버튼 클릭시 실행되는 함수
function test1(){
var myDog = new Dog('정완이형');
var area1 = document.getElementById("area1");
area1.innerHTML += "myDog instanceof Dog : " + (myDog instanceof Dog) + "<br>";
area1.innerHTML += "myDog instanceof Number : " + (myDog instanceof Number) + "<br>";
area1.innerHTML += "myDog instanceof String : " + (myDog instanceof String) + "<br>";
}
</script>
<hr>
<h3>new 키워드</h3>
<button onclick="test2();">new 확인하기</button>
<div id="area2" class="area"></div>
<script>
function Duck(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
function test2(){
// 생성자 함수를 new연산자로 객체를 찍어내는 용도로 사용할 때
var duck1 = new Duck("Mc", "Donald");
// duck1객체에서 생성자를 선언한 Duck함수에서 해당하는 생성자를 호출한다.
console.log(duck1);
var area2 = document.getElementById("area2");
area2.innerHTML += "첫 번째 오리의 full name은 " + duck1.firstName + duck1.lastName + "입니다. <br>";
// 생성자 함수를 일반 함수 처럼 써서 window객체를 사용할 떄
var duck2 = Duck("Donald", "Trump");
// duck2에는 아무 값도 반환되어 들어가지 않음(undefined)
console.log(duck2);
var area2 = document.getElementById("area2");
area2.innerHTML += "두 번째 오리의 full name은 " + window.firstName + window.lastName + "입니다. <br>";
}
</script>
</body>
<!-- 쉽게 말해 new 연산자를 안붙이면 객체가 아니라 그냥 함수 -->
</html>