Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Java Script/2011년스터디/서지혜

From ZeroWiki
Revision as of 06:56, 18 January 2011 by imported>rabierre
== 함수 ==
    • 중첩함수
  • 중첩 함수 예
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 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;