이머시브 코스 TIL 1주차 모음..
노션에 정리한거 옮겨오기
brew search 검색어
brew info 프로그램이름
brew install 프로그램이름
brew cask install 프로그램이름
brew update
brew upgrade 프로그램이름
brew uninstall 프로그램이름
brew list
런타임
: 프로그래밍 언어가 구동되는 환경(코드가 실행되는 곳)node.js
: 새로운 JavaScript 런타임node <file_name>
→ 작성한 코드가 node.js 환경에서 실행nvm을 통해 간단한 명령어로 node를 설치하고, 다양한 node version을 손쉽게 옮겨 다닐 수 있다
nvm 간단 사용법
nvm ls
: 현재 nvm을 통해 설치한 node version 확인node install 12.18.3
: 우리가 원하는 node version 설치(이전 것을 지우지 않고 설치가능)node use 버전넘버
: 다른 node version 사용nano 파일이름
: 파일열기^X
(Ctrl + X) : 파일 편집 후 종료 (Y
와 N
를 눌러서 저장여부 결정)^O WriteOut
: 파일 저장$ git config --global user.name "semin"
$ git config --global user.email 1234@gmail.com
$ git config --global core.editor nano
master 브랜치
: 처음 만든 저장소의 브랜치head
: 현재 사용 중인 브랜치의 선두 부분을 나타내는 이름브랜치 전환하기
git checkout 브랜치이름
작업공간 옮기기
git checkout -b 기능1
기능1 브랜치 생성+기능1 브랜치로 작업공간 이동
브랜치 병합하기
merge commit
병합 커밋
git merge <commit>
병합할 커밋 이름을 넣어 실행하면, 지정한 커밋 내용이 head
가 가리키고 있는 브랜치에 넣어짐
node.js에는 DOM이 존재하지 않는다.
dependencies
: 프로젝트가 돌아가기위해 반드시 필요한 모듈이 적힘npm install mocha --save-dev
, npm install --save react
devDependencies
: 프로젝트를 개발하는 환경에서 필요한 모듈이 적힘scripts
: npm으로 실행시킬 수 있는 명령어를 정의(CLI에서 사용가능한 명령)npm init
: npm이라는 패키지 매니저를 사용하겠다, 폴더를 npm을 쓰는 프로젝트로 정의하겠다
const add = (x, y) => {
return x + y;
}
const add = (x, y) => (x + y)
1.
const adder = function(x){
return function(y){
return x + y;
}
}
adder(5)(7) //12
2.
const adder = (x) => {
return (y) => {
return x + y;
}
}
3.
const adder = x => {
return y => x + y;
}
4.
const adder = x => y => x + y
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
sum(...numbers)
function sum(...theArgs) {
return theArgs.reduce((previous, current) => {
return previous + current;
});
}
sum(1,2,3)
// 배열
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2]; //[0, 1, 2, 3, 4, 5]
// 객체
let obj1 = { foo: 'bar', x: 42 };
let clonedObj = { ...obj1 }; // 그대로 {foo: "bar", x: 42} 들어감
[a, b, ...rest] = [10, 20, 30, 40, 50];
// a = 10, b = 20, rest = [30,40,50]
{a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40}
// a = 10, b = 20, rest = {c: 30, d: 40}
const 모듈이_담긴_변수 = require('모듈이름')
// script1.js
const module2 = require('./script2.js') // ./는 현재 디렉토리
// script2.js
console.log('this is module2')
// script1.js
const module2 = require('./script2.js')
console.log(module2) // 'this is module 2'
// script2.js
module.exports = 'this is module 2'
module.export
: 다른 스크립트 파일에서 내가 만든 모듈을 사용할 수 있도록 노출시키는 방법
module.exports vs exports
let counter1 = {
value: 0,
increase: function(){
this.value++
},
decrease: function(){
this.value--
},
getValue: function(){
return this.value
}
}
counter1.increase()
counter1.increase()
counter1.decrease()
counter1.getValue() //1
function makeCounter(){
return {
value: 0,
increase: function() {
this.value++ // 메소드 호출을 할 경우, this는 makeCounter 함수가 리턴하는 익명의 객체
},
decrease: function(){
this.value--
},
getValue: function(){
return this.value;
}
}
}
class Car{
constructor(brand, name, color){
this.brand = brand;
this.name = name;
this.color = color;
}
drive(){
// 운전을 구현하는 코드
}
}
let avante = new Car('hyundai', 'avante', 'black')
avante.color // 'black'
avante.drive() // 아반떼가 운전을 시작합니다
.call
, .apply
호출은 명시적으로 this를 지정하고 싶을 때 사용함func.call(this, ...arguments)
: 전개 문법을 사용해 인수가 담긴 배열을 전달func.apply(this, arguments)
: 유사 배열 형태의 args
만 받음let allDivs = document.querySelectorAll('div') // NodeList라는 유사배열
// allDivs를 this로 지정
[].map.call(allDivs, function(el)[
return el.className
})
// allDivs는 유사배열이므로 map 메소드가 존재하지 않음
// 그러나, Array prototype으로부터 map 메소드를 빌려와 this를 넘겨 map을 실행할 수 있음
bind 메소드(유용하게 잘사용됨)
.bind
는 .call
과 유사하게 this 및 인자를 바인딩하나, 당장 실행하는 것이 아닌 바인딩된 함수를 리턴하는 함수fn.bind(this값, 인자1, 인자2, ...)
btn.onclick = handleClick.bind(null, user);
→ 여기서 user는 객체let user = {
firstName: 'John',
sayHi(){
alert(`hello, ${this.firstName}!`)
}
};
setTimeout(user.sayHi, 1000) // Hello, undefined!
setTimeout(() => user.sayHi(), 1000); // Hello, John!
setTimeout(user.sayHi.bind(user), 1000); // Hello, John!
클래스와 인스턴스
OOP 주요 특징 4가지
const user = {
name: 'Kim',
age: 20
}
user.__proto__ === Object.prototype //true
function User(name){
this.name = name;
}
const kim = new User('kim');
kim.__proto__ === User.prototype // true
kim.__proto__.__proto__ === Object.prototype // true (Object는 가장 상위 객체)
pseudoclassical
let Human = function(name){
this.name = name;
}
// 메모리 낭비때문에 이렇게 작성
Human.prototype.sleep = function(){ console.log(this.name+' zzz') }
let Student = function(name){
Human.call(this, name); // Human.apply(this, arguments)
}
Student.prototype = Object.create(Human.prototype)
Student.prototype.constructor = Student
Student.prototype.learn = function(){}
const kim = new Student('kum');
kim.learn()
kim.sleep()
class
키워드를 쓰면 간단하게 작성할 수 있다.extends
: 상속하겠다super
: 부모 클래스에 전달class User{
constructor(name){
this.name = name;
}
eat(){ console.log('냠냠') }
}
class Student extends User{
constructor(name){
super(name)
}
learn(){ console.log('공부중..')}
}
const stu1 = new Student('kim')
stu1.learn() // 공부중..
stu1.eat() // 냠냠