반응형
//#apply call
var o = {}
var p = {}
function func() {
switch (this) {
case o:
document.write('o
');
break;
case p:
document.write('p
');
break;
case window:
document.write('window
');
break;
}
}
//func()함수 호출 --> 결과값 case window 걸려서 window 출력
func();
//func()함수에 표준메소드인 apply호출->this의 값이 o가 됨
func.apply(o);
//func()함수에 표준메소드인 apply호출->this의 값이 p가 됨
func.apply(p);
//# 상속
//Person 함수 생성(name-매개변수)
function Person(name) {
this.name = name;
}
Person.prototype.name = null;
Person.prototype.introduce = function() {
return 'My name is ' + this.name;
}
//함수 Programmer생성(name-매개변수)
function Programmer(name) {
this.name = name;
}
Programmer.prototype = new Person();
Programmer.prototype.coding = function() {
return 'hello world : Coding ';
}
//함수 Designer생성(name-매개변수)
function Designer(name){
this.name = name;
}
Designer.prototype = new Person();
Designer.prototype.design = function() {
return 'Beautiful : Design ';
}
//생성자 p1생성 Programmer 함수에 Computer 인자값 전달
var p1 = new Programmer('Computer');
//function Person 의 p1을 담은 객체 값 -> introduce 함수 호출
document.write(p1.introduce() + "
");
//function Programmer 의 p1을 담은 객체 값 -> coding 함수호출
document.write(p1.coding() + "
");
//생성자 p2생성 Programmer 함수에 Artist 인자값 전달
var p2 = new Designer('Artist');
document.write(p2.introduce() + "
");
document.write(p2.design() + "
");

반응형
'IT STUDY' 카테고리의 다른 글
| JavaScript - CSS호출, button, dom, 로직흐름 (0) | 2020.02.20 |
|---|---|
| JavaScript - 표준 내장 객체 & 레퍼객체 & 참조 (0) | 2020.02.20 |
| JavaScript - 객체지향 (0) | 2020.02.18 |
| JavaScript - 클로젯, 외부,내부함수,arguments (0) | 2020.02.17 |
| JavaScript - 함수처리, 함수호출(sum.apply()) (0) | 2020.02.17 |