'2014/01/17'에 해당되는 글 3건

  1. 2014.01.17 [JavaScript] JSON 객체
  2. 2014.01.17 [JavaScript] Date 객체
  3. 2014.01.17 [JavaScript] typeof, instanceof, constructor 프로퍼티

 

JSON

 

JSON은 자바스크립트에서 객체를 텍스트로 표현하는 방식.

 

JSON은 시스템 간의 데이터 교환의 수단으로 사용되고 있는 XML보다

용량이 작고, 파싱에 걸리는 시간이 절약되는 이유로 시스템 간의 데이터 교환에 많이 사용되고 있으며,

웹 환경에 최적화되어 있는 자바스크립트에서

바로 객체화해 사용할 수 있기 때문에 생산성과 편의성 또한 높음.

 

JSON은 숫자, 문자열, 불리언, 배열, 긱체를 표현할수 있음.

 

// JSON
var orderJSON = '{"name":"정도전","address":"서울시 종로구","jobs":[{"name":"조립", "pay":100},{"name":"분해", "pay":200}],"man":true}';
var order = JSON.parse(orderJSON);

console.log('order.name: ' + order.name);
console.log('order.address: ' + order.address);

for(var i=0; i < order.jobs.length; i++){
    for(prop in order.jobs[i]){
        console.log(prop + ': ' + order.jobs[i][prop]);
    }
}
console.log('order.man: ' + order.man);


/*
order.name: 정도전
order.address: 서울시 종로구
name: 조립
pay: 100
name: 분해
pay: 200
order.man: true
 * */
Posted by 달팽이맛나
,

 

Date

 

Date 객체는 리터럴 표현이 존재하지 않으며, new 연산자와 생성자 호출을 통해

객체를 생성해야함.

 

Date 객체는 1970년 1월 1일 0시 0분 0초를 기준으로 시간을 관리하며,(epoch time)

Date 객체의 월 지정값을 0~11

 

var d = new Date();
document.writeln(d);//Fri Jan 17 2014 13:17:32 GMT+0900 0 2014 
document.writeln(d.getFullYear());//2014 년
document.writeln(d.getMonth());//1 월 0~11임(0 :1월, 11:12월)
document.writeln(d.getDate());//17 일
document.writeln(d.getDay());//요일(0:일요일 ~ 6:토요일)

var d = new Date('2013/12/10');
var d = new Date(2012, 11, 10, 22, 10, 33, 500);
Posted by 달팽이맛나
,

 

 

프로토타입 상속을 한 사용자 정의 객체를 사용할때는

typeof, instanceof, constructor 프로퍼티, toString() 메소드등의

도움을 얻어 판달할 필요가 있음.

var Parent = function(){};
Parent.prototype.constructorname = 'Parent';

var Child = function(){};
Child.prototype = new Parent();
Child.prototype.constructor = Child;
Child.prototype.constructorname = 'Child';

var child = new Child();
document.writeln('typeof Parent : ' + typeof Parent + '
'); document.writeln('typeof Child : ' + typeof Child + '
'); document.writeln('typeof child : ' + typeof child + '
'); /* return: typeof Parent : function typeof Child : function typeof child : object * */ document.writeln('child instancdof Object : ' + ( child instanceof Object) + '
'); document.writeln('child instancdof Parent : ' + ( child instanceof Parent) + '
'); document.writeln('child instancdof Child : ' + ( child instanceof Child) + '
'); /* return: child instancdof Object : true child instancdof Parent : true child instancdof Child : true * */ document.writeln('child.constructor === Object: ' + ( child.constructor === Child) + '
'); document.writeln('child.constructor === Parent: ' + ( child.constructor === Parent) + '
'); document.writeln('child.constructor === Child: ' + ( child.constructor === Child) + '
'); /* return: child.constructor === Object: true child.constructor === Parent: false child.constructor === Child: true * */ function checkType(obj){ // null일 경우 문자열 'null' 반환 if(obj === null){ return 'null'; } //typeof가 'object'가 아닐경우 type반환 var type = typeof obj; if(type != 'object'){ return type; } //obj.toString()의 결과를 저장 var str = Object.prototype.toString.call(obj); // 생성자 이름 추출 var constructor = str.substring(8, str.length - 1); if(constructor != 'Object'){ return constructor; } if(obj.constructor == 'Object'){ return constructor; } // 사용자 정의 객체의 생성자 함수의 프로토타입 객체에 정의해 놓은 constructorname // 프로퍼티가 있으면 constructorname을 반환 if('constructorname' in obj.constructor.prototype) return obj.constructor.prototype.constructorname; return '객체의 타입을 알수 없습니다.'; } document.writeln('checkType(child) : ' + checkType(child) + '
'); /* return: checkType(child) : Child * */
Posted by 달팽이맛나
,