URL 해석
DNS(Domain Name System) 조회
해당 IP가 존재하는 서버로 이동
ARP(Address Resolution Protocol)를 이용하여 MAC 주소 변환
TCP 통신을 통해 Socket을 연다.
서버가 응답을 반환
브라우저가 렌더링함
이름에서 알 수 있듯이 HTTP는 암호화되지 않은 통신을 사용하고 HTTPS는 암호화된 통신을 사용한다.
HTTPS는 기존의 HTTP 통신 위에 SSL/TLS 프로토콜을 추가하여 암호화와 인증을 제공함
글로벌 서비스 같은 경우 시간이 매우 중요해짐 ⇒ 서비스에서 사용되는 시간을 용도에 맞춰서 기록할 필요가 있음
JS에서는 Date 객체나 date-fns, luxon 라이브러리를 사용할 수 있음
객체 리터럴
const Person = {
name: 'GunWoo',
move: function() {
console.log("이동합니다.");
},
}
Object
const person = new Object();
person.name = "GunWoo";
person.move = function () {
console.log("이동합니다.");
};
생성자 함수
function Person(name) {
this.name = name;
this.move = function () {
console.log("이동합니다.");
}
}
prototype이라는 프로퍼티를 가지고 있는데 prototype은 해당 객체의 프로토타입 객체를 가리킴prototype을 가지며, 이를 통해 다른 프로토타입과 연결되는 프로토타입 체인을 형성함prototype 프로퍼티에 정의된 프로퍼티와 메소드는 생성된 객체들이 상속받게 됨. 즉, prototype은 객체 생성자 함수의 프로토타입 객체를 가리킴function Person(name) {
this.name = name;
this.move = function () {
console.log("이동합니다.");
}
}
const p1 = new Person('gun');
const p2 = new Person('woo');
console.log(p1); // Person { name: 'gun', move: [Function (anonymous)] }
console.log(p2); // Person { name: 'woo', move: [Function (anonymous)] }
function Person(name) {
this.name = name;
Person.prototype.move = function () {
console.log("이동합니다.");
}
}
const p1 = new Person('gun');
const p2 = new Person('woo');
console.log(p1); // Person { name: 'gun' }
console.log(p2); // Person { name: 'woo' }
console.log(p1.__proto__); //{ move: [Function (anonymous)] }
부모 객체를 이용하여 프로토타입 함수 정의하기
function Person (name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name || "건우";
}
function Korean (name) {}
// 상위 객체의 함수가 링크됨 하지만 내부적으로 생성된 프로토타입 변수는 사용 불가
Korean.prototype = new Person();
const gun = new Person('gun');
const woo = new Korean('woo');
console.log(gun.getName()); // gun
console.log(woo.getName()); // 건우
부모 생성자를 빌려 쓰기
function Person (name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name || "건우";
}
function Korean (name) {
// apply함수를 통해 Korean 생성자 함수에 전달된 인자들을 Person 생성자 함수에 적용시키는 역할을 함
Person.apply(this, arguments);
}
Korean.prototype = new Person();
const gun = new Person('gun');
const woo = new Korean('woo');
console.log(gun.getName()); // gun
console.log(woo.getName()); // woo
기존 객체를 재활용함
const woo = {
name: 'woo',
getName: function () {
return this.name;
},
};
const gun = Object.create(woo);
gun.name = "gun"
console.log(woo.getName()); // woo
console.log(gun.getName()); // gun
console.log(woo.__proto__); // [Object: null prototype] {}
console.log(gun.__proto__); // { name: 'woo', getName: [Function: getName] }
<body>
<button>Click</button>
<script>
function handleClick(){
console.log('handleClick');
setTimeout(() => {
console.log('setTimeout');
handleClick();
}, 0);
}
const button = document.querySelector('button');
button.addEventListener('click', handleClick)
</script>
</body>
//handleClick과 setTimeout이 계속해서 출력되고 버튼 클릭등이 계속 가능하다. setTimeout 호출 → 콜백함수 호출 → 태스크 큐에 삽입 → 콜 스택 이동 → 콜백 함수 수행 → setTimeout 호출 ..... 반복<body>
<button>Click</button>
<script>
function handleClick(){
console.log('handleClick');
Promise.resolve(0)
.then(() => {
console.log('then');
handleClick();
})
}
const button = document.querySelector('button');
button.addEventListener('click', handleClick)
</script>
</body>
//handleClick과 setTimeout이 계속해서 출력되지만 버튼 클릭 등이 작동하지 않는다. 버튼 클릭 → 콜백함수가 테스크 큐에 삽입 → 콜 스택으로 콜백 함수 이동 → 프로미스 생성 및 프로미스가 완료되었을때 프로미스 then이 마이크로 테스크 큐에 삽입 → 마이크로 테스크 큐에 있는 then을 콜스택으로 이동 →모듈을 사용할 시 스크립트간 의존도 파악이 가능하고 실행 순서를 쉽게 제어할 수 있다.
<body>
<script type="module">
let a = 5;
let b = 10;
const c = a+b;
</script>
<script type="moudle">
alert(c); //오류 발생
</script>
</body><body>
<script type="module">
import './hello.js'; // Hello, World 출력
</script>
<script type="moudle">
import './hello.js'; // 한 번만 평가되기 때문에 출력되지 않음
</script>
</body><body>
<script type="module">
console.log(text); // DOM 생성이 안된 상태, 아직 text가 정의되지 않음
</script>
<script type="moudle">
console.log(text); // text 출력됨
</script>
<p id="text">Hello, World</p>
</body>/regexr/i ⇒ /: 시작, 종료 기호, regexr: 패턴, i: 플래그
RegexOne - Learn Regular Expressions - Lesson 1: An Introduction, and the ABCs
// 생성자 함수 방식
const regExp1 = new RegExp("^\d+");
const regExp2 = new RegExp("^\d+", "gi");
// 리터럴 방식
const regExp3 = /^\d+/;
const regExp4 = /^\d+/gi;const msg1 = "안녕하세요 제 전화번호는 010-8741-4496입니다.";
const msg2 = "아직 전화번호가 없어요..."
const regExp1 = /\d{3}-\d{3,4}-\d{4}/;
const regExp2 = new RegExp("\\d{3}-\\d{3,4}-\\d{4}");
console.log(regExp1.test(msg1)); // true
console.log(regExp1.test(msg2)); // false
console.log(regExp2.test(msg1)); // true
console.log(regExp2.test(msg2)); // falseconst msg1 = "안녕하세요 제 전화번호는 010-8741-4496입니다.";
const msg2 = "아직 전화번호가 없어요..."
const regExp1 = /\d{3}-\d{3,4}-\d{4}/;
const regExp2 = new RegExp("\\d{3}-\\d{3,4}-\\d{4}");
console.log(regExp1.exec(msg1));
console.log(regExp1.exec(msg2));
console.log(regExp2.exec(msg1));
console.log(regExp2.exec(msg2));
// 패턴이 존재하는 경우
// [
// '010-8741-4496',
// index: 14,
// input: '안녕하세요 제 전화번호는 010-8741-4496입니다.',
// groups: undefined
// ]
// 패턴이 존재하지 않는 경우
// null 반환const msg1 = "안녕하세요 제 전화번호는 010-8741-4496입니다.";
const msg2 = "아직 전화번호가 없어요..."
const regExp1 = /\d{3}-\d{3,4}-\d{4}/;
const regExp2 = new RegExp("\\d{3}-\\d{3,4}-\\d{4}");
console.log(msg1.match(regExp2));
console.log(msg2.match(regExp1));
// 출력결과는 exec 함수와 동일const msg2 = "010-1234-5677말고 010-2222-2222로 전환주세요."
const regExp1 = /(\d{3})-(\d{3,4})-(\d{4})/;
console.log(msg2.match(regExp1));
// [
// '010-1234-5677',
// '010',
// '1234',
// '5677',
// index: 0,
// input: '010-1234-5677말고 010-2222-2222로 전환주세요.',
// groups: undefined
// ]const msg1 = "안녕하세요 제 전화번호는 010-8741-4496입니다.";
const msg2 = "010-1234-5677말고 010-2222-2222로 전환주세요."
const regExp1 = /\d{3}-\d{3,4}-\d{4}/g; // 옵션으로 g를 붙이면 패턴에 맞는 모든 문자열이 변경됨
const regExp2 = new RegExp("\\d{3}-\\d{3,4}-\\d{4}");
console.log(msg1.replace(regExp2, "전화번호")); // 안녕하세요 제 전화번호는 전화번호입니다.
console.log(msg2.replace(regExp2, "전화번호")); // 전화번호말고 010-2222-2222로 전환주세요.
console.log(msg2.replace(regExp1, "전화번호")); // 전화번호말고 전화번호로 전환주세요.const msg1 = "안녕하세요 제 전화번호는 010-8741-4496입니다.";
const msg2 = "010-1234-5677말고 010-2222-2222로 전환주세요."
const regExp1 = /\d{3}-\d{3,4}-\d{4}/g; // 옵션으로 g를 붙이면 패턴에 맞는 모든 문자열이 변경됨
const regExp2 = new RegExp("\\d{3}-\\d{3,4}-\\d{4}");
console.log(msg1.search(regExp2)); // 14
console.log(msg2.search(regExp1)); // 0, 옵션에 상관 없이 처음 매칭된 값을 반환함function getAntSeq(n) {
let seq = '1';
for (let i = 1; i < n; i++) {
// \1*은 캡처된 문자와 동일한 문자들의 반복을 의미
seq = seq.replace(/(.)\1*/g, match => match.length + match[0]);
}
return seq;
}
console.log(getAntSeq(5)); // 출력: 111221⇒ 이러한 문제를 해결하기위해 서버와 클라이언트 간의 인증은 JWT같은 별도 토큰을 이용하고 쿠키관리 는 클라이언트 자체적으로 지속적인 데이터 관리 용도로 많이 사용함