// κ°μ²΄
const o1 = new Object();
const o2 = {}; // 리ν°λ΄ νκΈ°. κ°νΈνλ€
o1.name = "νκΈΈλ";
o1.hello = () => {
console.log("hello");
};
o2.name = "μ무κ°";
o2.hello = () => {
// μ€νλ μ€μκ° μλ€λ©΄ μ΄λ»κ² νμ
νμ§?
console.log("μλ
");
};
μμ λ°©μμ μ¬μ©νλ©΄ νλ‘νΌν°μ μ€νλ μ€μκ° μμ λ νμΈν μ μλ€.
-> μμ±μ ν¨μλ₯Ό μ¬μ©νλ€.
// μμ
function User(name, age) {
// ν΄λΉ κ°μ²΄μ νλ‘νΌν° μ§μ
this.name = name; // μ¬κΈ°μ thisλ windowλ€.
this.age = age;
}
varλ‘ μ μΈν μ μ λ³μ > windowμ νλ‘νΌν°
μμ±μ ν¨μλ μ λλ‘ μΌλ° ν¨μμ²λΌ κ·Έλ₯ νΈμΆνλ©΄ μλλ€.
μμ±μ ν¨μλ λ°λμ new
μ°μ°μμ κ°μ΄ μ¬μ©ν΄μΌ νλ€.
const o3 = new User("νκΈΈλ", 20);
console.log(o3);
new -> λΉ κ°μ²΄ μμ±
User ν¨μ μ€ν
맀κ°λ³μλ₯Ό newκ° λ§λ κ°μ²΄μ λ£λλ€.(μ΄κΈ°ν)
κ° κ°μ²΄μ μμ±μλ₯Ό ꡬ체μ μΌλ‘ νμΈνλ €λ©΄ μμ±μ ν¨μλͺ μ μ¬μ©νλ€.
console.log(typeof desk); // object
console.log(typeof today); // object
console.log(typeof list2); // object
// object > ꡬ체μ μΌλ‘ νμΈνλ €λ©΄ > μμ±μ ν¨μλͺ
μ¬μ©
console.log(today.constructor.name); // Date
console.log(list2.constructor.name); // Array
// μ΅λͺ
ν¨μ μ¬μ©
const o5 = {
name: "νκΈΈλ",
age: 20,
info: function () {
// info: () => { // -> μ΄κ±°λ thisκ° μλλ€. functionμ μ¨μΌ νλ€.
console.log(this.name, this.age);
},
};
o5.info();
// μ€λͺ
ν¨μ μ¬μ©
const o6 = {
name: "νΈνΈνΈ",
age: 27,
info: m1,
};
function m1() {
console.log(this.name, this.age);
}
o6.info(); //o6.name, o6.age
m1(); // ν¨μμ μν: window.m1(); > window.name, window.age
μ€λͺ
ν¨μλ₯Ό μ¬μ©νλ©΄ μ¬μ©νλ νκ²½μ λ°λΌ κ²°κ³Όκ° λ¬λΌμ§λ€.
function f1() {
console.log("f1");
}
f1();
const f2 = function () {
console.log("f2");
};
f2();
(function () {
console.log("f2_1");
})(); // μ¦μ νΈμΆ. μμκ° λ μΌλ°μ μ΄λ€.
const f3 = () => {
// μλ°λ ->
console.log("f3");
};
f3();
μλ νν
alert(event.target.value);
alert(event.srcElement.value);
alert(event.currentTarget.value);
alert(this.value);
μ λ νν > λΉμΆμ²
alert(document.getElementById("btn1").value);
*** νμ΄ν ν¨μλ μ΄λ€ μμΌλ‘ μ¬μ©(νΈμΆ)λλ κ·Έ μμμμ thisλ νμ window κ°μ²΄λ₯Ό κ°λ¦¬ν¨λ€.
alert(this.value); // undefined this > window κ°μ²΄
for (let i = 0; i < list.length; i++) {
console.log(list[i]);
}
νλ‘νΌν° νμ
for (let p in list) {
console.log(p); // μΈλ±μ€ μΆλ ₯
}
forEach() - ν₯μλ forλ¬Έ
list.forEach(function (num) {
console.log(num);
});
flist.forEach((f, index, list) => console.log(f, index, list));
console.log(list.map((n) => (n % 2 == 0 ? "μ§μ" : "νμ")));
console.log(flist.map((item) => item.length));
flist.map((item) => item.length).forEach((length) => console.log(length));
const olist2 = names.map((name) => {
return {
name: name,
age: 20,
address: "μμΈμ",
};
});
console.log(olist2);
console.log(list.filter((num) => num % 2 == 0));
console.log(flist.find((item) => item.length >= 3));
λ‘컬 μ€μΉ > νμΌ λ€μ΄λ‘λ + μ°Έμ‘°
<script src="../asset/js/jquery-1.12.4.js"></script>
CDN μ€μΉ > μ격 νμΌ μ°Έμ‘°
<script
src="https://code.jquery.com/jquery-1.12.4.js"
integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
crossorigin="anonymous"
></script>
document.all.txt1.value = "BOM";
document.getElementById("txt1").value = "DOM";
jQuery("#txt1").val("jQuery");
=> BOM, DOM <-> jQuery νΈν λΆκ°. κ°κ°μ΄ μλ‘ λ€λ₯Έ κ°μ²΄μ΄λ©°, κ°μμ λ©μλ μ¬μ©ν΄μΌ νλ€.
λ©μλ νΈνμ΄ λμ§ μμμ νλ³νμ ν΄μΌνλ€.
// BOM or DOM κ°μ²΄ > (νλ³ν) > jQuery κΈ°λ₯ μ¬μ©
jQuery(a).val("ok"); // μ’
μ’
μ΄λ€
//jQuery κ°μ²΄ > (νλ³ν) > BOM or DOM κ°μ²΄
// - jQuery κ°μ²΄λ‘λΆν° [index]λ₯Ό μ¬μ©ν΄μ κΊΌλ΄λ μκ° μμ μλ°μ€ν¬λ¦½νΈ κ°μ²΄κ° λ°νλλ€.
c[0].value = "ok"; // μ μμ΄λ€
<input>
btn.addEventListener('click',function(){});
μλ λ κ°κ° κ°λ€.
jQuery("#btn1").css("color", "blue");
$("#btn2").css("color", "orange");
$("#btn1").click(function () {
alert();
});
$("#btn2").on("click", function () {
alert();
});
$("#btn1").off("click"); // μ΄λ²€νΈ μ κ±°
// μλμ κ°μ΄ μ¬μ©νλ©΄ λ°λ³΅λ¬Έ μμ΄ ν΄λΉ ν΄λμ€κ° λ€μ΄μλ λμμ μ΄λ²€νΈλ₯Ό 건λ€.
$(".btn").click(function () {
alert(event.target.value);
});
$("#box").hide("fast");
$("#box").fadeOut("fast");
$("#box").slideUp("fast");
animate > λμ > μ«μν
κ°μ²΄ μ¬μ© μ λ΄κ° μνλ λλ‘ νλ‘νΌν°λ₯Ό μΆκ°ν μ μλ€.
const obj = {
width: "400px",
height: "400px",
};
$("#box").animate(obj);
$("#box").animate(
{
// margin-left: '+=50px'
// marginLeft: '+=50px'
"margin-left": "+=50px",
},
1000,
function () {
// alert("μ λλ©μ΄μ
μ΄ λλ λ νκ³ μΆμ μΌ");
$("h1").text("jQuery Effect " + n);
n++;
if (n > 5) {
$("#btn1").off("click");
}
}
);
$("#box")
.slideup()
.slideDown()
.fadeOut()
.fadeIn()
.animate({ borderWidth: "10px" });