[[pagelist(^JavaScript/2011년스터디)]] [[TableOfContents]] = 1월 11일 = {{{ 짜가계산기  
}}} = 1월 18일 = * 중첩함수 - 중첩 함수 예 {{{ function duple_test() { function inner() { /* body comes here */ } } // good }}} - 내부 익명함수는 접근할 수 없기 때문에 안됨 {{{ function duple_test() { function () { /* body comes here */ } // anonymous inner class can't be reached } // bad }}} - 이 방법은 된다 {{{ function duple_test() { var one = function () { } // anonymous but reachable } }}} - 이것도 된다 {{{ function duple_test() { (function () { /* body comes here */ })(); // 바로 실행해 버리니 괜찮음 } }}} - 여러번 중첩되어도 된다 {{{ function one(){ function two() { function three() { /* body comes here */ } } } // good }}} * 함수 파라메터 {{{ /* Arguments 객체 테스트 */ function arg_test(one, two) { document.write(one); document.write(arguments[0]); document.write(two); document.write(arguments[1]); document.write(arguments.length); document.write(arguments[3]); // 존재하지 않는 배열을 참조한다면? -> undefined arguments[3] = 5; // 내부에서 파라메터 삽입 가능!! document.write(arguments[3]); } arg_test(3, 4); }}} * 클래스 생성 {{{ /* 클래스 만들기 */ function Class_test(one, two) { this.one = one; this.two = two; } /* 객체 생성 */ var instance = new Class_test(1, 2); }}} * 상속 * 자바스크립트의 상속은 객체지향언어의 상속과 다르다. {{{ function MyClass(id, name){ this.id = id; this.name = name; } var class_test = new MyClass("1", "test"); MyClass.prototype.name1 = "name1"; class_test.name2 = "name2"; document.write(class_test.name1); // 여기서 상속이 일어나야함.... } }}} * 슈퍼클래스화, 서브클래스화 {{{ // 슈퍼클래스 function Parent(m, f) { this.mother = m; this.father = f; } }}} - 서브클래스화 1 {{{ // 자식클래스 1 function Child(m, f, b, s) { Parent.call(m, f); // 생성자 체이닝 this.brother = b; this.sister = s; } Child.prototype = new Parent(); Child.prototype.constructor = Child; }}} - 서브클래스화 2 {{{ // 자식클래스 2 function Child(m, f, b, s) { this.superclass(m, f); // 생성자 체이닝 this.brother = b; this.sister = s; } Child.prototype.superclass = Parent; }}} * 정규표현식 {{{ /* 정규표현식 */ var sample_string = "string for regular expression pattern matching. perl, python, ruby, javascript"; sample_string.match(/s/); // 지역검색 sample_string.match(/s/g); // 전역검색 sample_string.match(/perl|python/g); // ["perl", "python"] sample_string.match(/p(erl|ython)/); // ["perl", "erl"] 왜 이렇게 나올까 sample_string.match(/p(erl|ython)/g); // ["perl", "python"] 이러면 되요 sample_string.match(/p[erl|ython]/); // ["pr"] 왜 이게나와아아아아ㅏ아 }}} * 네임스페이스 {{{ var org = {}; org.zeropage = 0; org.zeropage.namespace = 0; // 이건되고 //org.zeropage.namespace.function1 = "" // 이건 왜 안됨? org.zeropage.namespace = function() { document.write("네임스페이스 안의 함수 테스트"); } org.zeropage.namespace(); var simple.zeropage = org.zeropage.namespace; simple.zeropage(); /* TypeError: Object 0 has no method 'namespace' */ var org = {}; org.zeropage = 0; org.zeropage.namespace = 0; // 이건되고 //org.zeropage.namespace.function1 = 0 // 이건 왜 안됨? org.zeropage.namespace = function() { document.write("네임스페이스 안의 함수 테스트"); } org.zeropage.namespace(); var simple.zeropage = org.zeropage.namespace; simple.zeropage(); }}}