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.

번호 제목 글쓴이 날짜 조회 수
186 브라우저의 스크롤을 따라다니는 레이어 두번째 file 황제낙엽 2002.12.20 542
185 소스 보기 막기 황제낙엽 2005.11.18 541
184 Barcode Detection API 황제낙엽 2023.08.06 539
183 Jasmine 테스트 및 CI 구축 가이드 황제낙엽 2016.11.16 539
182 체크박스에 체크된 항목 개수 구하기 황제낙엽 2023.06.10 538
181 Page Refresh/Reload 황제낙엽 2007.08.24 538
180 팝업창을 다시 띄우지 않는 소스 황제낙엽 2005.07.16 533
179 Reference Count (순환참조) 황제낙엽 2011.11.24 529
178 XMLHttpRequest.timeout 황제낙엽 2018.11.03 528
177 자바스크립트 내장 함수 활용하기 황제낙엽 2005.04.25 519
176 문자열에서 역슬래시(backslash) 문자와 유니코드(Unicode)에 대한 고찰 file 황제낙엽 2021.06.03 518
175 XP 에서 input type=text 와 input type=password 의 사이즈가 틀리게 보일때 황제낙엽 2004.08.04 517
174 code compressor & decompressor 황제낙엽 2015.01.02 509
173 CryptoJS 를 이용한 암호화 황제낙엽 2023.02.15 504
172 감추기, 보이기 화면 예제 황제낙엽 2008.04.14 504
171 자바스크립트에서 이벤트 처리에 대한 일련의 흐름 황제낙엽 2008.09.18 503
170 무지개링크 (rainbowlink) file 황제낙엽 2005.07.16 501
169 이미지 로드 코드 황제낙엽 2009.06.27 500
168 JavaScript TDD by 강규영 황제낙엽 2007.11.26 498
167 (Bard) FileReader 로 여러개의 파일을 read 하는 법 file 황제낙엽 2023.08.23 496