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

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 달팽이맛나
,
// Car 생성자 함수의 프로토타입 객체는 new 연산자와 Object 생성자 함수 호출을 통해 생성된 객체이며
// 프로토타입 객체에 constructor 프로퍼티는 자동으로 Car 생성자 함수를 참조한다.
var Car = function(){};

Car.prototype = {
   startEngine : function(){
       document.writeln('시동을 겁니다.
'); }, accelerate : function(){ document.writeln('속도를 올립니다.
'); }, decelerate : function(){ document.writeln('속도를 줄입니다.
'); }, stopEngine : function(){ document.writeln('시동을 끕니다.
'); } }; // Bus 생성자 함수의 프로토타입 객체는 new 연산자와 Object 생성자 함수 호출을 통해 생성된 객체이며, // 프로토타입 객체에 constructor 프로퍼티는 자동으로 Bus 생성자 함수를 참조합니다. var Bus = function(){}; // Bus() 생성자 함수의 prototype 프로퍼티가 new 연산자와 Car() 생성자 함수 호출을 통해 생성된 객체를 참조하면 // Car() 생성자 함수의 프로토타입 멤버를 상속하게 됩니다. Bus.prototype = new Car(); // 프로토타입 객체가 Car() 생성자 함수를 통해 만들어졌기 때문에 constructor 프로퍼티는 // Car() 생성자 함수를 참조하게 됩니다. 따라서 constructor 프로퍼티를 Bus() 생성자 함수로 변경합니다. Bus.prototype.constructor = Bus; Bus.prototype.startNavigation = function(){ document.writeln('네비게이션 안내를 시작합니다.
'); }; Bus.prototype.stopNavigation = function(){ document.writeln('네비게이션 안내를 종료합니다.
'); }; var bus = new Bus(); bus.startEngine(); bus.startNavigation(); bus.accelerate(); bus.decelerate(); bus.stopNavigation(); bus.startEngine(); /* result : 시동을 겁니다. 네비게이션 안내를 시작합니다. 속도를 올립니다. 속도를 줄입니다. 네비게이션 안내를 종료합니다. 시동을 겁니다. */
Posted by 달팽이맛나
,

블랙야크 강태선 나눔재단연탄나눔, 태풍지원

 

 

 

 

 

동진레저와블랙야크 임직원 100여명이 관악구 신림동에

방문하여 소외계층과 독거 노인분들에게 사랑의 연탄

4 9백장을 전달하였다고 전하였습니다.

 

블랙야크 강태선 나눔재단의 강태선씨는 올해 추위가

일찍 찾아와 봉사활동을 서둘러 준비하였다고 하며

작지만 어려운 분들에게 힘과 용기가 되기를 바란다며

이러한연탄나눔 봉사활동을 하였다고 전하였습니다.

 

 

이날 주민들은 다과를 준비해와 강태선 나눔재단과

따뜻한 시간을 보냈다고 전하였습니다.

 

블랙야크 강태선 나눔재단은 지난 9월에 출범을 하였고

산악인 유족지원, 녹색환경 조성, 저속득층 지원,

소외계층 지원 등으로 사회적 문제를 해결할수 있도록

공익사업을 펼치고 있습니다.

 

 

블랙야크강태선나눔재단 필리핀 재해지역 물품지원

 

사회복지법인인블랙야크 강태선 나눔재단은 계열사와

협력사가 같이 필리핀 하이옌 태풍피해 지역에 10억원

정도의 물품을 지원하겠다고 전하였습니다.

신발은 9550켤레이며 의류는 약 3000여벌이라고 합니다.

 

 

12월에 1차 구호 물품을 지원 하였고 12월 말에 피해지역에

추가적인 구호 물품을 전달할 것이라고 전하였습니다.

 

 

강태선나눔재단 강태선 이사장은 블랙야크는국내 뿐만이

아니라 해외에도 어려운 여건속에 생활하는 저소득층을

위해 기업의 사회적 책임을 다할것이라고 전하며 단발성

보여주기 식이 아닌 꾸준하고 실질적인 지원을 하도록

하겠다며 최선을 다하겠다고 전하였습니다.

 

또한 지난 4일에는 네팔 교육시설을 지원하였고 문화보급을

위하여북스인터네셔널과 업무협약도 맺었습니다.

 

북스인터네셔널은 문맹퇴치와 교육 개선을 목표로 프로젝트를

진행하는 단체입니다.

 

Posted by 달팽이맛나
,
try{
정상적으로 처리되어야할 코드
런타임에서 에러가 발생하여 에외가 발생하거나,
throw 문을 통해 예외를 직접 발생시킬수 있다.

}catch(e){
예외 발생시 실행될 코드
  예외와 관련된 정보를  e 변수를 통해 참조
  예외를 처리 or 무시할수 있고, 
  throw문을 통해 예외를 다시 발생시킬수 있습니다. 
}finally{
try, catch 실행 완료후 무조건 실행이 필요한 코드
}


var x = 5;
var result;

try{
    result = x * y;
    document.writeln(result);
} catch(e){
    document.writeln(e);
}finally{
    document.writeln('ok');
}
/*
result : 
ReferenceError: y is not defined 
ok 
*/

try{
    result = x / 0;
    document.writeln(result);
    throw new Error('0으로 나눌 수 없습니다.');
} catch(e){
    document.writeln(e);
    document.writeln('
'); }finally{ document.writeln('ok'); document.writeln('
'); } /* result : 5 Error: 0으로 나눌 수 없습니다. ok */ try{ var calculator; calculator = calculator || {add : function(op1, op2){ return op1 + op2; }}; calculator && document.write(calculator.add(2,3)); calculator && document.write(calculator.sub(2,3)); }catch(e){ document.write(e); } /* result : 5 TypeError: calculator.subtract is not a function */


Posted by 달팽이맛나
,

[JavaScript] for...in

JavaScript 2014. 1. 10. 17:45


for...in 문은 객체의 프로퍼티나 배열의 원소에 대해 순서대로 반복처리를 실행


변수에는 객체로부터 나온 프로퍼티명이 저장되거나, 배열로부터 나온 인덱스번호가 저장된다.


for( 변수 in "객체" or "배열"){


}

var arr = [ {name: '정도전', age: 50}, {name: '이성계', age: 60}, {name: '정몽주', age: 54}, {name: '이방원', age: 43} ]; for(var idx in arr){ for(var prop in arr[idx]){ document.writeln(prop + ' : ' + arr[idx][prop]); if(prop == 'age'){ break; } } }


Posted by 달팽이맛나
,