고군분투 개발 공부
방명록 RSS 태그 글쓰기 관리자
 
[FreeCodeCamp] Testing Objects for Properties
JavaScript 2022-12-23 10:42:11

Sometimes it is useful to check if the property of a given object exists or not. We can use the .hasOwnProperty(propname) method of objects to determine if that object has the given property name. .hasOwnProperty() returns true or false if the property is found or not.

Example

const myObj = {
  top: "hat",
  bottom: "pants"
};

myObj.hasOwnProperty("top");
myObj.hasOwnProperty("middle");

The first hasOwnProperty returns true, while the second returns false.

 

Modify the function checkObj to test if an object passed to the function (obj) contains a specific property (checkProp). If the property is found, return that property's value. If not, return "Not Found".

 

  • Passed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") should return the string pony.
  • Passed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") should return the string kitten.
  • Passed:checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") should return the string Not Found.
  • Passed:checkObj({city: "Seattle"}, "city") should return the string Seattle.
  • Passed:checkObj({city: "Seattle"}, "district") should return the string Not Found.
  • Passed:checkObj({pet: "kitten", bed: "sleigh"}, "gift") should return the string Not Found.

 

\

 

function checkObj(objcheckProp) {
  if (obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
  } else {
    return "Not Found";
  }
}

 

 



프로퍼티
JavaScript 2022-12-21 19:42:38

요소 노드에 대한 이동 프로퍼티

element.children 자식 요소 노드 element의 자식 요소 모음(HTMLCollection)
element.firstElementChild 자식 요소 노드 element의 첫 번째 자식 요소 하나
element.lastElementChild 자식 요소 노드 element의 마지막 자식 요소 하나
element.parentElement 부모 요소 노드 element의 부모 요소 하나
element.previousElementSibling 형제 요소 노드 element의 이전(previous) 혹은 좌측(left)에 있는 요소 하나
element.nextElementSibling 형제 요소 노드 element의 다음(next) 혹은 우측(right)에 있는 요소 하나

 

모든 노드에 대한 이동 프로퍼티

node.childNodes 자식 노드 node의 자식 노드 모음(NodeList)
node.firstChild 자식 노드 node의 첫 번째 자식 노드 하나
node.lastChild 자식 노드 node의 마지막 자식 노드 하나
node.parentNode 부모 노드 node의 부모 요소 하나
node.previousSibling 형제 노드 node의 이전(previous) 혹은 좌측(left)에 있는 노드 하나
node.nextSibling 형제 노드 node의 다음(next) 혹은 우측(right)에 있는 노드 하나

 

DOM 트리를 구성할 때..

브라우저가 HTML 코드를 해석할 때 각 코드들은 상황에 맞게 node를 생성하고 DOM 트리를 구성하는데,

HTML 태그요소 노드가 되고, 문자들텍스트 노드, 그리고 주석주석 노드로 DOM 트리에 반영됨

<!DOCTYPE HTML>
<html>
<head>
  <title>JavaScript</title>
</head>
<body>
  I Love JavaScript
  <!-- I Love Codeit -->
</body>
</html>

 

 



[JavaScript] 객체가 비어있는지 확인하기 - 프로퍼티 체크
JavaScript 2022-12-15 17:44:57

객체에 프로퍼티가 하나도 없는 경우 true, 그렇지 않은 경우 false를 반환해주는 함수 isEmpty(obj)를 만들기

 

function isEmpty(obj){
	for (let key in obj){
    return false;
    }
    return true;
   }

 

이 간단한 코드가 뭐라고




이 사이트에는
넥슨코리아에서 제공한 넥슨 Lv.1 고딕 Regular체,
카페24가 제작한 아네모네체,
Cadson Demak가 디자인한 Kanit체,
Sandoll이 디자인한 나눔고딕체가
적용되어 있습니다.
멋진 폰트를 무료로 제공해주셔서 감사합니다.

Copyleft ⓒ bskyvision (블루스킨 v1.2)