properties 값이 함수일 경우 method라고 부른다
var sub = {
subject : 'java',
credit : 3,
info : function(){
return sub.subject + ' - ' + sub.credit + '학점';
},
info2 : function(){
return this.subject + ' - ' + this.credit + '학점';
}
};
console.log('과목평 : ' + sub.subject);
console.log('학점 : '+ sub.credit);
console.log(sub.info());
console.log(sub.info2());
console.log('');
delete sub.credit;
console.log('학점 : '+ sub.credit);
sub.credit = 4;
console.log('학점 : '+ sub.credit);
sub.info2 = function(){
return '과목명 : ' + this.subject + ' / ' + this.credit + '학점';
}
console.log(sub.info2());
console.log('');
object literal을 사용하면 하나의 객체만 생성하므로 같은 properties을 가지고 있는 객체가 여러개일 경우 비효율적이다 그래서 생성자 함수를 사용하게 되면 new를 통해 새로운 객체를 계속 생성할 수 있기 때문에 효율적이다
function Subject(name, credit){
this.name = name;
this.credit = credit;
this.info = function(){
return '과목명 : ' + this.name + ' / ' + this.credit + '학점';
};
}
var html = new Subject('html',3);
console.log('과목명 : '+ html.name);
console.log('과목명 : '+ html.credit);
console.log(html.info());
function Rectangle(width, height){
this.width = width;
this.height = height;
}
Rectangle.prototype.area = function(){
return this.width * this.height;
}
var rectA = new Rectangle(3, 4);
console.log('넓이 : ' + rectA.area());
console.log('');
for(var p in rectA){
console.log(`${p} : ${rectA[p]}`);
}
class Subject {
constructor(name, credit){
this.name = name;
this.credit = credit;
}
get getName(){ return this.name; }
set setCredit(credit){
this.credit = credit;
}
info(){
return '과목명 : ' + this.name + ' / ' + this.credit + '학점';
}
}
let js = new Subject('javascript', 3);
console.log('과목명 : ' + js.getName);
console.log(js.info());
console.log('');
js.setCredit =4;
console.log(js.info());
let a =10;
let b = new Number(10);
console.log('a 타입 : ' + typeof a);
console.log('b 타입 : ' + typeof b);
console.log('');
let c = 123.45;
console.log('c : ' + c.toFixed(1));
console.log('');
let d = '20' + '10';
console.log('d : '+ d);
if(isNaN(d)){
console.log('숫자가 아니에요 ~');
}else{
console.log('숫자 입니다 ...');
}
console.log('');
d = 'a';
console.log('d : '+ d);
if(isNaN(d)){
console.log('숫자가 아니에요 ~');
}else{
console.log('숫자 입니다 ...');
}
console.log('');
''안에 적어도 숫자라면 숫자임을 알려줌
parseFloat()
: 문자열을 파싱하여, 문자열의숫자 부분을 실수로 변환
isInteger()
: 정수인지 확인해주는 함수
Math.min()
: 가장 작은 수를 반환하는 함수 ( '' or " "안에 적어도 숫자면 포함하여 찾아줌)
Math.max()
: 가장 큰 수를 반환하는 함수 ( '' or " "안에 적어도 숫자면 포함하여 찾아줌)
Math.random()
: 0.0 ~ 1.0 사이의 랜덤값을 생성
// 0~3까지 랜덤
let ra = Math.random();
console.log(Math.floor(ra * 4));
console.log('');
// 1~5까지 랜덤
for(let i=0; i<5; i++){
let data = Math.random();
console.log(Math.floor(data * 5)+ 1);
Math.round()
: 소수점 첫째자리에서 반올림
Math.floor()
: 소수점자리를 절삭
console.log(Math.floor(12.34));
console.log(Math.floor(12.56));
console.log(Math.floor(-3.8));
console.log(Math.floor(-3));
console.log('');
let now = new Date();
console.log(now);
console.log(now.toGMTString());
console.log('');
console.log(now.getFullYear() + '년');
console.log(now.getMonth() + '월');
console.log(now.getDate() + '일');
console.log(now.getHours() + '시');
console.log(now.getMinutes() + '분');
console.log(now.getSeconds() + '초');
console.log(now.getDay() + '요일'); // 일요일(0) ~ 토요일(6)
console.log('');
let date = new Date(2023, 10, 12);
console.log(date);