본문 바로가기

프론트엔드 공부/javscript

(34)
정적 메서드와 정적 프로퍼티 정적 메서드는 특정 클래스 인스턴스가 아닌 클래스 '전체’에 필요한 기능을 만들 때 사용할 수 있습니다. 인스턴스끼리 비교해주는 메서드나 팩토리 메서드를 만들 때 정적 메서드가 쓰입니다. 정적 메서드는 클래스 선언부 안에 위치하고 앞에 static이라는 키워드가 붙습니다. 정적 프로퍼티는 데이터를 클래스 수준에 저장하고 싶을 때 사용합니다. 정적 프로퍼티 역시 개별 인스턴스에 묶이지 않습니다. **참고자료 https://ko.javascript.info/static-properties-methods 정적 메서드와 정적 프로퍼티 ko.javascript.info
정규식표현 + .find(),.findIndex,includes() //.find(), .findIndex(), includes() const numbers =[1,2,3,4]; const fruits = ['apple','banana','cherry']; const a = fruits.find(elem=> /^b/.test(elem) //B로 시작하는 문자열 찾아서 boolean ) console.log(a); //banana const b = fruits.findIndex(elem=>/^c/.test(elem) //c로 시작하는 문자열 index ) console.log(b); //2 const c = fruits.includes('apple'); console.log(c); //true /**text()와 정규식표현 //RegExp.prototype.test() /..
map과 forEach 차이 //map & forEach //forEach는 아이템 갯수만큼 콜백함수를 반복(return값x) //map은 아이템 갯수만큼 콜백함수를 반복 + 콜백 내부에서 return 키워드로 //반환 데이터를 새로운 배열로 만들어 사용 가능 const numbers =[1,2,3,4]; const fruits = ['apple','banana','cherry']; const a = fruits.forEach(function(fruit,index){ console.log(`${fruit}-${index}`); }); //apple-0 bannan-1 cherry-2 console.log(a); //undefined const b = fruits.map(function(fruit,index){ return{ id:i..
number - toFixed,parseInt,parseFloat,Math //toFixed(2) 인수로 소수점 몇번째까지 고정할지 정함, 단 type이 string으로 변환됨 const pi = 3.141592535 console.log(pi); const str = pi.toFixed(2); console.log(str);//3.14 console.log(typeof str); //string //숫자와 관련된 대표적인 전역함수parseInt, parseFloat const integer = parseInt(str); const float = parseFloat(str); console.log(integer,float); //3 3.14 console.log(typeof integer, typeof float);//number number //내장객체 Math //Math...
String prototype - indexOf,length,slice,replace,match,trim //String.prototype.indexOf() const result = 'Hello world'.indexOf('world'); const result2 = 'Hello world'.indexOf('wow'); console.log(result); //6 console.log(result2); //-1 //문자열없을 경우 -1 //String.length 스트링객체 문자길이 알고싶을때 사용하는 prototype //String.slice(x번째부터,y-1까지추출) : 추출의 개념 !! //**비교String.splice() //String.replace(a,b) a를 찾아서 b로 교체 const str = 'hello world'; console.log(str.replace('world','mag..
class 상속과 확장 class A기본구문 > class B extends A 에서의 super 개념 > class C extends A에서의 super와 매개변수추가 //class의 상속과 확장 class Vehicle{ constructor(name,wheel){ this.name = name; this.wheel = wheel; } } const myVehicle = new Vehicle('운송수단',2); console.log(myVehicle); //확장(상속) //super의 매개변수는 class Vehicle의 매개변수에서 받아주는 값 class Bicycle extends Vehicle{ constructor(name,wheel){ super(name,wheel); } } const myBicycle = new..
일반함수와 화살표함수에서 this 정의 차이점 setTimeOut이나 setInterval 같은 함수 사용시 콜백으로 화살표함수 사용하는것이 더 활용서이 뛰어나다 //this 일반함수, 화살표함수 차이 //일반(normal) 함수는 호출 위치에 따라 this 정의 !! //화살표(arrow) 함수는 자신이 선언된 함수 범위 내에서 this 정의 !! function User(name){ this.name = name; } User.prototype.normal = function(){ console.log(this.name); } User.prototype.arrow = () => { console.log(this.name); } //객체생성자함수 const mag = new User('phter'); mag.normal(); //pheter mag..
변수유효범위(variable scope) 변수 유효범위 (variable scope): 변수가 블록 스코프 안에 있거나 값이 할당되기 전에 선언되면 유효범위 밖이라고 볼 수 있다. let, const는 블록레벨의 유효범위를 가진다 고 말한다. (var은 함수레벨의 유효범위를 가진다. ) function scope(){ //console.log(a); if(true){ //console.log(a); let a = 123; console.log(a); } //console.log(a); }