클래스와 모듈
자바스크립트 완벽가이드 클래스와 모듈을 학습한다
function Range(from, to){ this.from = from; this.to = to; } Range.prototype = { include : function(){ //something } // 배열처럼 상속시키고자 하는 함수를 위와 동일하게 작성해나간다. }
var someFunction = function() {}; var protoTypeInSomeFunction = someFunction.prototype; var constructorInProtoTypeInSomeFunction = protoTypeInSomeFunction.constructor; console.log(someFunction == constructorInProtoTypeInSomeFunction) // trueRange.prototype = { constructor : Range, //이런 식으로 명시적으로 역참조를 위해 설정할 수 있다. //etc... }Range.prototype.etc1 = function(){ //something } Range.prototype.etc2 = function(){ //something } Range.prototype.etc3 = function(){ //something }
function duckTyping(object){ for(var i = 1; i < arguments.length; i++){ //object 의 각각의 인수에 대하여 var arg = arguments[i]; switch(typeof arg) { case 'string': if(typeof o[arg] !== "function") return false; // 접근된 요소가 문자열일 경우 메서드의 이름을 확인한다. continue; case 'function' : arg = arg.prototype; // 접근된 요소가 함수라면 프로토타입을 사용하게 한다. case 'object' : for(var m in arg) { // 객체라면 모든 프로퍼티에 대해서 검사를 해야한다. if (typeof arg[m] !== "function") { continue; } //객체의 각 프로퍼티에 대해서 검사 if (typeof o[m] !== "function") { return false; } // 메서드가 아니라면 건너뛴다. } } } }
Last updated