'JavaScript'에 해당되는 글 12건

  1. 2011.11.10 callback.call()에 대해서
  2. 2011.11.07 jQuery() 함수 인자들 정리
새로 만드는 메서드(함수)의 매개변수로 콜백함수를 받을때, 함수 정의내부에서는 callback.call로 호출한다. 이때 call의 첫번째는 현재의 되는 객체가 들어가고, 2번째 부터는 매개변수를 나열하면 된다.

예를 들면 다음과 같다.

function Thing(name) {

    this.name = name;

}


Thing.prototype.doSomething = function(callback) {

    callback.call(this, v1, v2);

}


function foo() {

    alert(this.name+v1+v2);

}


var t = new Thing('Joe');

t.doSomething(foo);

 
이렇게 하면 Joe라고 찍힌다.

눈여겨 볼 곳은 callback.call(this) 여기인데, 이 this는 Thing객체를 의미하고, 이는 foo() 내에서 this로 참조 할 수 있다. 그냥 직관적으로 말한다면, 콜백함수(여기서는 foo()) 안에서 this로 쓰일 객체를 첫번째 인자로 넘긴다는 것이다.
 
http://stackoverflow.com/questions/2190850/javascript-create-custom-callback

ㄹㄹㄹ 

'JavaScript' 카테고리의 다른 글

HTML5 File Drag & Drop API  (0) 2013.10.30
Simple JavaScript Inheritance  (2) 2012.01.02
JSONP로 데이타 처리하기.  (1) 2011.12.12
Posted by altvirus
,
사실 그냥 쓰고는 있는데, 생각보다 꽤 여러가지가 있어서 한번 정리해둘 필요가 있겠당..

http://api.jquery.com/jQuery/


1. $(셀렉터, 컨텍스트)
- $('p'); // p요소들 선택

2. DOM 엘리먼트(XML 포함)

$(document.form)

$.post('url.xml', function(data) {
  var $child = $(data).find('child');
})

3. jQuery 객체
dom_obj = document.form[0];
$source = $(dom_obj);
$clone = $($source
);

객체를 복사한다. 이때 처음의 객체(dom_obj) 레퍼런스는 유지된다.


4. 텅빈 값
o =  $();
하면 그냥 텅빈 jQuery 객체 리턴. length 값은 0으로 ..

5. 일반 객체
var foo = {foo:'bar', hello:'world'};
var $foo = $(foo);

$foo.prop('foo'); // bar

$foo.prop('foo', 'foobar');
var test2 = $foo.prop('foo'); // foobar

============================ 
6. $(html 문자, [프로퍼티 정의객체])

$('<a/>');

$('<a href="http://jquery.com"></a>');

el = $('1<br/>2<br/>3'); // returns [<br>, "2", <br>] 
 
$('<input />', {    type: 'text',    name: 'test' })


7. $(callBack함수)

DOM의 로딩이 완료되었을때 실행되는 함수
$(function(){
   // Document is ready
 });




'JavaScript > jQuery' 카테고리의 다른 글

Mouse Wheel  (0) 2012.11.27
jQuery Carousel - modular  (2) 2012.11.27
has(), is()  (0) 2012.07.22
jQuery 플러그인과 그 메서드들을 만들기 위한 기본구조..  (0) 2012.01.01
jQuery 플러그인들 수집  (0) 2011.12.28
jQuery Template  (0) 2011.12.27
Posted by altvirus
,