js03.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var name = '가';
let name1 = 'A';
console.log(name, name1);
console.log(this.name, this.name1);
console.log(window.name, window.name1);
function test() {
var name = '나';
console.log(name);
console.log(this.name);
}
test();
window.test();
let obj = {
name: '다',
test: function() {
console.log(name);
console.log(this.name);
console.log(window.name);
}.bind(this)
}
obj.test();
</script>
</body>
</html>
- PL은 규칙을 정해야한다.
ex) JS 오브젝트에 접근할 때는 this를 사용하자 등
js04.html - 1
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function count() {
var cnt = 0;
return ++cnt;
}
console.log(count());
console.log(count());
var cnt = 0;
function count() {
return ++cnt;
}
console.log(count());
console.log(count());
</script>
</body>
</html>
-> 호이스팅에 따라 위 값이 nan으로 뜬다.
-> 함수형 사용을 지양하는 것이 좋다.
js04.html - 2
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function count() {
var cnt = 0;
return ++cnt;
}
console.log(count());
console.log(count());
var cnt = 0;
function count1() {
return ++cnt;
}
cnt = 100;
console.log(count1());
console.log(count1());
// 클로저(Closure) D.P.
function count2() {
var count = 0;
return function() {
return ++count;
}
}
let cl = count2(); // cl은 함수이다.
console.log(cl());
cpmsole.log(cl());
let cl2 = count2(); // cl은 함수이다.
console.log(cl2());
cpmsole.log(cl2());
// 커링(currying) D.P.
function add(x, y) {
return x + y;
}
function add2(x){
return function(y) {
return x + y;
}
}
console.log(add(1, 2));
console.log(add2(1)(2));
function curry(fn) {
return function(x) {
return x.map(fn);
}
}
console.log(curry((x) => x*x)([1, 2, 3, 4, 5]));
// Momoization D.P.
let cnt = 0;
function fib(n) {
cnt++;
if (n < 2) return n;
return fib(n-2) + fib(n-1);
}
let r = fib(5);
console.log(cnt, r);
let result = [];
cnt1 = 0;
function fib2(n) {
cnt1++;
if (n < 2) return result[n] = n;
if (result[n] == undefined) {
result[n] = fib2(n-2) + fib2(n-1);
}
return result[n];
}
r = fib2(5);
console.log(cnt1, r, result);
</script>
</body>
</html>
-> 지역변수를 사용하여 문제를 해결해볼 수 있다.
js05.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT35Qfaw==" crossorigin="anonymous"></script>
</head>
<body>
<script>
let result="";
$.get('msg.json', function(data) {
result = data;
console.log(result);
});
console.log(result);
</script>
<!-- 자바 스크립트는 순서를 보장하지 않는다. = 비동기식 -->
</body>
</html>
npm install -g npm
npx create-react-app react-app
cd react-app
npm start
https://legacy.reactjs.org/docs/cdn-links.html
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>