재밌는 이벤트의 세계, JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 빈 객체 생성 : 프로토타입
let people = {};
// 변수 추가
people.name = "자바학생";
people.gender = "여자";
// 함수 추가
people.saySomething = function(msg){
document.write("<h1>" + msg + "</h1>");
};
// 객체에 추가한 변수를 이용하여 출력하기
document.write("<h1>" + people.name + "님은 " + people.gender + "입니다.</h1>");
// 객체에 추가한 함수 사용하기
people.saySomething("JavaScript!!!!");
</script>
</body>
</html>
객체이름.함수이름 = function(파라미터){};
함수가 대입된 변수는 그 자신이 함수처럼 동작해요.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 일반적인 함수 선언
function myFunction(){
document.write("<h1>Hello, JavaScript</h1>");
}
// JavaScript 함수와 변수가 동급이다.
// 함수의 이름을 변수에 대입할 수 있다.
let value = myFunction;
// 함수가 대입된 변수는 그 자신이 함수처럼 동작한다.
value();
document.write("<h1>---------------------------------</h1>");
// 함수를 변수에 대입할 수 있기 때문에, 처음부터 변수에 함수의 내용을 대입할 수 있다.
let value2 = function(){
document.write("<h1>Hello, JavaScript2</h1>");
};
value2();
</script>
</body>
</html>
this.변수이름;
this.함수이름();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 객체 생성
let people = {};
// 변수 추가
people.name = "자바학생";
people.gender = "여자";
people.sayName = function(){
document.write("<h1>" + this.name + "</h1>");
};
people.sayGender = function(){
document.write("<h1>" + this.gender + "</h1>");
};
people.getName = function(){
return this.name;
};
people.getGender = function(){
return this.gender;
};
people.sayName();
people.sayGender();
document.write("<h1>" + people.getName() + "님은 " + people.getGender() + "입니다.</h1>");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// key 와 value관계
let grades = {
'abc' : 10,
'def' : 6,
'ghr' : 80
};
document.write(grades.abc + "<br>");
document.write(grades.def + "<br>");
document.write(grades.ghr + "<br>");
document.write("------------------------<br>");
document.write(grades['abc'] + "<br>");
document.write(grades['def'] + "<br>");
document.write(grades['ghr'] + "<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let grades = {
'list' : {'aaa' : 10, 'bbb' : 6, 'ccc' : 80},
'show' : function(){
document.write("<h1>Hello, World</h1>")
}
};
grades.show();
document.write(grades.list.aaa + "<br>");
document.write(grades.list.bbb + "<br>");
document.write(grades.list.ccc + "<br>");
</script>
</body>
</html>
출처
https://media.giphy.com/media/qqtvGYCjDNwac/giphy.gif
https://media.giphy.com/media/26tPplGWjN0xLybiU/giphy.gif