day08
<script>
/*
javascript κ°μ²΄μμ± κ³Όμ
- λΉ κ°μ²΄μ μμ±
-> μλ¬΄λ° κΈ°λ₯μ΄ μλ μνμ λΉ κ°μ²΄λ₯Ό μμ±
μ΄ μνκ° prototype μΈλ€.
- λ³μμ μΆκ°
- ν¨μμ μΆκ°
λΉ κ°μ²΄ μμ±
- let people = {};
λ³μμ μΆκ°
- people.name = "μλ°νμ";
- people.gender = "μ¬μ";
*/
let people = {};
// κ°μ²΄μμ λ³μ(λ©€λ²λ³μ, νλ‘νΌν°)
people.name = "μλ°νμ";
people.gender = "μ¬μ";
document.write("<h1>" + people.name + " λμ "
+ people.gender + " μ
λλ€</h1>");
</script>
π‘ κ²°κ³Όκ°
people.name = μλ°νμ
people.gender = μ¬μ
<script>
let grades = {
'abc' : 10,
'def' : 6,
'ghr' : 80
};
// document.write(grades['abc'] + "<br>");
// document.write(grades.def + "<br>");
for( key in grades ){
document.write("<li>key : " + key
+ " ,value : " + grades[key] + "</li>");
}
</script>
π‘ κ²°κ³Όκ°
<script>
/*
κ°μ²΄μ΄λ¦.ν¨μμ΄λ¦ = fucntion(νλΌλ―Έν°){
.. ν¨μ ꡬν λΆλΆ..
return κ°;
};
- λ©μλ μμμ κ°μ²΄ μμ νμ©νκΈ°
- κ°μ²΄ μμ ν¬ν¨λ λ©μλμμ λ€λ₯Έ λ©μλλ₯Ό
νΈμΆνκ±°λ, νλ‘νΌν°(λ©€λ²λ³μ)λ₯Ό νμ©νκ³ μ
νλ κ²½μ°μλ this ν€μλλ₯Ό μ¬μ©νλ€.
this.λ³μμ΄λ¦;
*/
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.saySomething = function(msg){
document.write("<h1>" + msg + "</h1>");
};
people.getName = function(){
return this.name;
};
people.getGender = function(){
return this.gender;
};
people.sayInfo = function(){
document.write("<h1>" + this.getName()
+ "λμ " + this.getGender() + "μ
λλ€.</h1>");
};
// κ°μ²΄μμ λ©μλ νΈμΆ
people.sayName();
people.sayGender();
people.saySomething("Hello javascript");
people.sayInfo();
</script>
π‘ κ²°κ³Όκ°
people.sayName(); = μλ°νμ
people.sayGender(); = λ¨μ
people.saySomething(msg) = // msgμ νλΌλ―Έν° ν¬μ ν λ΄μ© (μΆλ ₯ν λ΄μ©)
people.saySomething("Hello javascript");
people.sayInfo(); = μλ°νμλμ λ¨μμ λλ€.
<script>
// let person = {};
// person.name = 'kjh';
// person.introduce = function() {
// return 'My name is ' + this.name;
// }
// document.write(person.introduce());
let person = {
'name' : 'kjh2',
'introduce' : function(){
return 'My name is ' + this.name;
}
}
document.write(person.introduce());
</script>
π‘ κ²°κ³Όκ°
<script>
// let person = {};
// person.name = 'lkh';
// person.introduce = function() {
// return 'My name is ' + this.name;
// }
// document.write(person.introduce());
let person = {
'name' : 'lkh2',
'introduce' : function(){
return 'My name is ' + this.name;
}
}
document.write(person.introduce());
</script>
π‘ κ²°κ³Όκ°