donaricano-btn

자바스크립 날짜/시간 더하기,빼기,차


1. 더하기/빼기

- get메소드로 값을 구한 후 수정된 값을 set한다

1
2
3
4
5
6
7
8
var date = new Date('2016/12/25 08:23:13');
 
 console.log(date);
 console.log(date.toLocaleString());
 date.setMonth(date.getMonth()+3);
 console.log(date.toLocaleString());
 date.setDate(date.getDate() - 20);
 console.log(date.toLocaleString());

Sun Dec 25 2016 08:23:13 GMT+0900 (대한민국 표준시)

2016. 12. 25. 오전 8:23:13

2017. 3. 25. 오전 8:23:13

2017. 3. 5. 오전 8:23:13


2. 날짜 차

1
2
3
4
5
var date = new Date('2016/12/25');
var date1 = new Date('2016/08/20');
 
var diff = (date.getTime() - date1.getTime()) / (1000*60*60*24);
console.log(diff+'일');
 

- getTime()에서 타임스탬프 값을 구한 뒤 차를 구한다

- 일의 차이를 구한다면 '1000*60*60*24' 나눔

- 127



블로그 이미지

리딩리드

,
donaricano-btn

자바스크립트 n 진수 구하기

- toString()을 이용하여 원하는 진수를 구할 수 있다


1. 예제

1
2
3
4
5
6
7
8
var num = 128;
 
//2진수
console.log(num.toString(2)); //  10000000
//8진수
console.log(num.toString(8));// 200
//16진수
console.log(num.toString(16));// 80

블로그 이미지

리딩리드

,
donaricano-btn

객체 판단 (instanceOf, IsPrototypeOf , In)


1.  instanceOf

- 객체를 생성한 생성자 함수, 프로토타입 체인을 알 수 있다

- 객체명 instanceOf 생성자

1
2
3
4
5
6
7
8
var Person = function(){};
var Programer = function(){};
 
 Programer.prototype = new Person();
 var test = new Programer();
 console.log(test instanceof Programer); //true
 console.log(test instanceof Person);  //true
 console.log(test instanceof Object);  /true


2.  isPrototypeOf

-  참조하고 있는 프로토타입확인

1
2
3
4
5
6
7
8
9
var Person = function(){};
var Programer = function(){};
 
Programer.prototype = new Person();
var test2 = new Programer();
 
console.log(Person.prototype.isPrototypeOf(test2)); //true
console.log(Programer.prototype.isPrototypeOf(test2)); //true
console.log(Object.prototype.isPrototypeOf(test2)); //true
 


3. in

- 객체 생성시(생성자 방법, 리터럴) 해당 객체의 멤버 유무

1
2
3
4
5
6
7
8
9
10
11
var obj = {
    getName : function(){
 
    },
    getAge : function(){
 
    }
}
 
console.log('getName' in obj); //true
console.log('getTall' in obj); //false
 

블로그 이미지

리딩리드

,
donaricano-btn

자바스크립트의 클래스와 생성자의 강제적 호출 


1. 클래스

- 자바스크립트는 클래스가 존재하지 않는다. 그렇기 때문에 함수를 이용하여 클래스를 정의한다

1
2
3
4
5
6
7
8
9
10
var Person = function(name){
    this.name = name;
    this.toString = function(){
        return 'Person:' + this.name;
    }
}
 
 var ps = new Person('Hee');
 console.log(ps.name); //Hee
 console.log(ps.toString()); //Person : Hee

1) 정의 법칙

a. 이름은 대문자로 시작

b. 프로퍼티는 this.이름

- this는 생성된 인스턴스를 가리킨다

c. 메소드는 함수형 프로퍼티

- 값이 함수 객체인 프로퍼티를 메소드로 동작시킴

- 메소드를 생성자에 정의하는 것은 효율적으로 좋지않다(프로토타입 사용)

d. 반환값은 필요없다


2. new 키워드 없이 객체 인스턴스 생성?

1) 문제 : 만약 new 키워드 없이 객체를 만들려고 한다면 어떻게 될까?

1
2
3
4
5
6
7
8
9
10
11
var Person = function(name){
     this.name = name;
     this.toString = function(){
         return 'Person:' + this.name;
     }
 }
 
  var ps = Person('Hee');
  console.log(ps);
  console.log(name);
  console.log(ps.name);
 

- 실행결과 : undefined, Hee, error

- new 키워드가 없다면 그건 생성자가 아닌 보통 함수가 되어버린다

- Person 객체는 생성되지 않고, 전역 변수 name이 생성된다

2) 해결

- 이런 new 키워드 실수를 방지하고자 생성자 안에 임의 소스를 삽입한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Person = function(name){
     if(!(this instanceof Person)){
         return new Person(name);
     }
     this.name = name;
     this.toString = function(){
         return 'Person:' + this.name;
     }
 }
 
  var ps = Person('Hee');
  console.log(ps);
  console.log(name);
  console.log(ps.name);
 

- this전역객체 혹은 undefined일 것이다

- this를 이용하여 Person 객체가 아니라면 자동으로 new 로 객체를 만드는 소스를 삽입한다


블로그 이미지

리딩리드

,