TIL 23.10.13

ν™©μ€ν•˜Β·2023λ…„ 10μ›” 12일
0

TIL

λͺ©λ‘ 보기
97/146

πŸ“ŒToday I Learned

JavaScript

ex64

// 객체
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

μ‹€λͺ… ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ μ‚¬μš©ν•˜λŠ” ν™˜κ²½μ— 따라 κ²°κ³Όκ°€ 달라진닀.

ex65

ν™”μ‚΄ν‘œ ν•¨μˆ˜

  • μžλ°”μ˜ λžŒλ‹€ > 읡λͺ… 객체의 좔상 λ©”μ†Œλ“œλ₯Ό ν‘œν˜„ν•˜λŠ” 기술
  • μžλ°”μŠ€ν¬λ¦½νŠΈ > 읡λͺ… ν•¨μˆ˜λ₯Ό ν‘œν˜„ν•˜λŠ” 기술

  1. ν•¨μˆ˜ μ„ μ–Έλ¬Έ
function f1() {
  console.log("f1");
}
f1();
  1. ν•¨μˆ˜ ν‘œν˜„μ‹(λ¦¬ν„°λŸ΄) == 읡λͺ… ν•¨μˆ˜
const f2 = function () {
  console.log("f2");
};
f2();

(function () {
  console.log("f2_1");
})(); // μ¦‰μ‹œ 호좜. μœ„μ—κ°€ 더 μΌλ°˜μ μ΄λ‹€.
  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));

map()

  • μš”μ†Œλ₯Ό λ‹€λ₯Έ κ°’μœΌλ‘œ λ³€ν™˜ > μƒˆλ‘œμš΄ λ°°μ—΄ λ°˜ν™˜
  • 가곡 + λ³€ν™˜
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);

filter() > 닀쀑값 λ°˜ν™˜

  • λ‚΄κ°€ μ œμ‹œν•œ 쑰건을 λ§Œμ‘±ν•˜λŠ” μ• λ“€λ§Œ κ±ΈλŸ¬μ„œ λ„˜κΈ°κΈ°
console.log(list.filter((num) => num % 2 == 0));

find()

  • μš”μ†Œ 검색 > 단일값 λ°˜ν™˜
  • 첫번째 λ§Œλ‚˜λŠ” κ°’λ§Œ λ¦¬ν„΄ν•œλ‹€.
console.log(flist.find((item) => item.length >= 3));

jQuery

ex01

jQuery

  • 2006λ…„
  • jQuery is a fast, small, and feature-rich JavaScript library.

jQuery μ„€μΉ˜

  1. 둜컬 μ„€μΉ˜ > 파일 λ‹€μš΄λ‘œλ“œ + μ°Έμ‘°
    <script src="../asset/js/jquery-1.12.4.js"></script>

  2. CDN μ„€μΉ˜ > 원격 파일 μ°Έμ‘°

<script
	src="https://code.jquery.com/jquery-1.12.4.js"
	integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
	crossorigin="anonymous"
></script>

μš”μ†Œμ— μ ‘κ·Όν•˜κΈ°

  1. BOM > BOM 트리
document.all.txt1.value = "BOM";
  1. DOM > getXXX λ©”μ†Œλ“œ or DOM 트리(Axis) or querySelectorXXX
document.getElementById("txt1").value = "DOM";
  1. jQuery > jQuery()
  • document.querySelectorAll()
jQuery("#txt1").val("jQuery");

ex02

jQuery() ν•¨μˆ˜

  • CSS μ„ νƒμž or λ‹€λ₯Έ ν‘œν˜„μ„ μ‚¬μš©ν•΄μ„œ νƒœκ·Έλ₯Ό κ²€μƒ‰ν•œλ‹€.
  1. jQuery()둜 찾은 κ²°κ³ΌλŠ” 항상 배열이닀.
  2. λ°˜ν™˜κ°’μ— 무언가λ₯Ό μ μš©ν•˜λ©΄ 항상 μžλ™μœΌλ‘œ 루프가 λ™μž‘λ˜μ–΄ 각 μš”μ†Œλ§ˆλ‹€ μ μš©λœλ‹€.
  3. μžλ°”μŠ€ν¬λ¦½νŠΈ 객체 > (λ³€ν™˜) > jQuery 객체

=> BOM, DOM <-> jQuery ν˜Έν™˜ λΆˆκ°€. 각각이 μ„œλ‘œ λ‹€λ₯Έ 객체이며, 각자의 λ©”μ†Œλ“œ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.
λ©”μ†Œλ“œ ν˜Έν™˜μ΄ λ˜μ§€ μ•Šμ•„μ„œ ν˜•λ³€ν™˜μ„ ν•΄μ•Όν•œλ‹€.

λ³€ν™˜

// BOM or DOM 객체 > (ν˜•λ³€ν™˜) > jQuery κΈ°λŠ₯ μ‚¬μš©
jQuery(a).val("ok"); // μ’…μ’… μ“΄λ‹€

//jQuery 객체 > (ν˜•λ³€ν™˜) > BOM or DOM 객체
// - jQuery κ°μ²΄λ‘œλΆ€ν„° [index]λ₯Ό μ‚¬μš©ν•΄μ„œ κΊΌλ‚΄λŠ” μˆœκ°„ 순수 μžλ°”μŠ€ν¬λ¦½νŠΈ 객체가 λ°˜ν™˜λœλ‹€.
c[0].value = "ok"; // 잘 μ•ˆμ“΄λ‹€

ex03

  1. BOM
  • <input>
  • btn.onclick = function(){}
  1. DOM
  • btn.addEventListener('click',function(){});
  1. jQuery
  • 이벀트 ν•¨μˆ˜

μ•„λž˜ 두 κ°œκ°€ κ°™λ‹€.

jQuery("#btn1").css("color", "blue");
$("#btn2").css("color", "orange");

jQuery 이벀트 ν•¨μˆ˜

  1. μ „μš© 이벀트 ν•¨μˆ˜
  • 객체.μ΄λ²€νŠΈν•¨μˆ˜(μ½œλ°±ν•¨μˆ˜)
$("#btn1").click(function () {
  alert();
});
  1. λ²”μš© 이벀트 ν•¨μˆ˜
  • 객체.on(이벀트λͺ…, μ½œλ°±ν•¨μˆ˜)
$("#btn2").on("click", function () {
  alert();
});
$("#btn1").off("click"); // 이벀트 제거

// μ•„λž˜μ™€ 같이 μ‚¬μš©ν•˜λ©΄ 반볡문 없이 ν•΄λ‹Ή ν΄λž˜μŠ€κ°€ λ“€μ–΄μžˆλŠ” λŒ€μƒμ— 이벀트λ₯Ό 건닀.
$(".btn").click(function () {
  alert(event.target.value);
});

ex04_effect

jQuery Effect Function

  1. hide(time), show(time), toggle(time)
  2. fadeOut(time), fadeIn(time), fadeToggle(time)
  3. slideUp(time), slideDown(time), slideToggle(time)
  4. animate() > μ‚¬μš©μž μ •μ˜
$("#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" });
profile
μ°¨κ·Όμ°¨κ·Ό ν•˜λ‚˜μ”©

0개의 λŒ“κΈ€