More actions
imported>rabierre No edit summary |
imported>rabierre No edit summary |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
DeleteThisPage | |||
== 함수 == | |||
** 중첩함수 | ** 중첩함수 | ||
* 중첩 함수 예 | * 중첩 함수 예 | ||
| Line 17: | Line 18: | ||
var one = function () { | var one = function () { | ||
} // anonymous but reachable | } // anonymous but reachable | ||
} | |||
* 이것도 된다 | |||
function duple_test() { | |||
(function () { | |||
/* body comes here */ | |||
})(); // 바로 실행해 버리니 괜찮음 | |||
} | } | ||
* 여러번 중첩되어도 된다 | * 여러번 중첩되어도 된다 | ||
| Line 27: | Line 34: | ||
} // good | } // good | ||
** | ** 함수 파라메터 | ||
/* Arguments 객체 테스트 */ | /* Arguments 객체 테스트 */ | ||
function arg_test(one, two) { | function arg_test(one, two) { | ||
| Line 38: | Line 45: | ||
document.write(arguments.length); | document.write(arguments.length); | ||
document.write(arguments[3]); // 존재하지 않는 배열을 참조한다면? | document.write(arguments[3]); // 존재하지 않는 배열을 참조한다면? -> undefined | ||
arguments[3] = 5; // 파라메터 삽입 가능 | arguments[3] = 5; // 내부에서 파라메터 삽입 가능!! | ||
document.write(arguments[3]); | document.write(arguments[3]); | ||
} | } | ||
arg_test(3, 4); | 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 | |||
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 | |||
function Child(m, f, b, s) { | |||
this.superclass(m, f); // 생성자 체이닝 | |||
this.brother = b; | |||
this.sister = s; | |||
} | |||
Child.prototype.superclass = Parent; | |||
Latest revision as of 07:05, 18 January 2011
DeleteThisPage == 함수 ==
- 중첩함수
- 중첩 함수 예
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
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
function Child(m, f, b, s) {
this.superclass(m, f); // 생성자 체이닝
this.brother = b;
this.sister = s;
}
Child.prototype.superclass = Parent;