sitelink1 | http://www.java-samples.com/showtutorial.php?tutorialid=829 |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | http://1 |
extra_vars4 | ko |
extra_vars5 | |
extra_vars6 | sitelink1 |
The call() method
The call() method is the method most similar to the classic object-masquerading method. Its first argument is the object to be used for this. All other arguments are passed directly to the function itself.
For example:
function sayColor(sPrefix, sSuffix) {
alert(sPrefix + this.color + sSuffix);
};
var obj = new Object();
obj.color = “red”;
//outputs “The color is red, a very nice color indeed. “
sayColor.call(obj, “The color is “, “, a very nice color indeed. “);
In this example, the function sayColor() is defined outside of an object, and it references the this keyword even though it is not attached to any object. The object obj is given a color property equal to “red”. When call() is, well, called, the first argument is obj, which indicates that the this keyword in sayColor() should be assigned the value of obj. The second and third arguments are strings. They are matched up with the prefix and suffix arguments of sayColor(), resulting in the message “The color is red, a very nice color indeed.” being displayed.
To use this with the object masquerading method of inheritance, just replace the three lines that assign, call, and delete the new method:
function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.call(this, sColor);
this.name = sName;
this.sayName = function () {
alert(this.name);
};
}
Here, you want the this keyword in ClassA to be equal to the newly created ClassB object, so this is passed in as the first argument. The second argument is the color argument, the only one for either class.
The apply() method
The apply() method takes two arguments: the object to be used for this and an array of arguments to be passed to the function. For example:
function sayColor(sPrefix, sSuffix) {
alert(sPrefix + this.color + sSuffix);
};
var obj = new Object();
obj.color = “red”;
//outputs “The color is red, a very nice color indeed. “
sayColor.apply(obj, new Array(“The color is “,”, a very nice color indeed.”));
This is the same example as before, but now the apply() method is being called. When apply() is called, the first argument is still obj, which indicates that the this keyword in sayColor() should be assigned the value of obj. The second argument is an array consisting of two strings, which are matched up with the prefix and suffix arguments of sayColor(). This also results in the message “The color is red, a very nice color indeed.” being displayed.
This method is also used in place of the three lines to assign, call, and delete the new method:
function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.apply(this, new Array(sColor));
this.name = sName;
this.sayName = function () {
alert(this.name);
};
}
Once again, you pass this in as the first argument. The second argument is an array with just one value: color. You can, alternatively, pass in the entire arguments object of ClassB as the second argument of the apply() method:
function ClassB(sColor, sName) {
//this.newMethod = ClassA;
//this.newMethod(sColor);
//delete this.newMethod;
ClassA.apply(this, arguments);
this.name = sName;
this.sayName = function () {
alert(this.name);
};
}
Of course, passing in the object of the arguments only works if the order of the arguments in the superclass constructor is exactly the same as the order of the arguments in the subclass. When this is not the case, you must create a separate array to place the arguments into the correct order. You could also use the call() method.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
137 | 라이노 (Rhino) 관련 링크 모음 | 황제낙엽 | 2008.07.21 | 74 |
136 | |= 비트 OR 대입 연산자 (복합대입연산자) | 황제낙엽 | 2017.03.15 | 73 |
135 | JScript Memory Leaks | 황제낙엽 | 2011.11.28 | 69 |
134 | HTTP Content-Type 정리 | 황제낙엽 | 2013.09.30 | 68 |
133 | 자바스크립트 숫자형 체크 함수 (isFinite() 함수 와 isNaN() 함수) | 황제낙엽 | 2011.12.13 | 67 |
132 | innerHTML | 황제낙엽 | 2005.12.19 | 67 |
131 | 아이디 생성 조건 검사 자바스크립트 모듈 | 황제낙엽 | 2004.11.18 | 67 |
130 | SpiderMonkey와 Tamarin | 황제낙엽 | 2008.07.14 | 66 |
129 | 상속과 Super 로의 접근 | 황제낙엽 | 2012.09.18 | 64 |
128 | 콜백지옥과 비동기제어 | 황제낙엽 | 2021.02.18 | 63 |
127 | XMLHttpRequest.setRequestHeader | 황제낙엽 | 2013.09.30 | 62 |
126 | 소숫점을 포함한 반올림 | 황제낙엽 | 2008.12.11 | 62 |
125 | Understanding delete | 황제낙엽 | 2011.11.28 | 61 |
124 | Iframe 내의 페이지 접근방법 | 황제낙엽 | 2009.11.12 | 59 |
123 | XMLHttpRequest 제대로 활용하기 | 황제낙엽 | 2017.08.01 | 58 |
122 | Javascript 내장객체 Array (배열) | 황제낙엽 | 2008.10.13 | 57 |
121 | charcode 32와 160 차이 (javascript char 160 to 32) | 황제낙엽 | 2019.05.11 | 55 |
120 | 체인 생성자(생성자 체인), 프로토타입 체인 그리고 생성자 재지정 | 황제낙엽 | 2009.08.12 | 55 |
119 | 다양한 WYSIWYG 에디터 | 황제낙엽 | 2011.11.13 | 54 |
118 | State of ECMAScript 4 ('07 12) | 황제낙엽 | 2008.07.14 | 54 |