-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_jquery_basic_4_1a
// jQuery-3.2.1.js - Code Line 10246
window.jQuery = window.$ = jQuery;jQuery์ $๋ ๋์ผํ ๋ณ์๋ค
jQuery ์์ค ์ฝ๋ ๋ถ์ 2. ํจ์ ๊ฐ์ฒด $
// jQuery-3.2.1.js - Code Line 94
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context );
},jQuery ๋ณ์๋ jQuery.fn.init ์์ฑ์๋ฅผ ํธ์ถํ์ฌ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ๋ฐํํ๋ ํจ์๋ก ์ ์๋์ด ์๋ค. (new๋ฅผ ์ฌ์ฉํ๋ ํจ์์ด๋ฏ๋ก ์์ฑ์๋ก ๋ถ๋ฅธ๋ค)
Help understanding jQuery's jQuery.fn.init Why is init in fn
jQuery.fn์ jQuery.prototype์ ๋ณ์นญ์ด๋ค. ์ด๊ฒ์ ํ์ดํ์ ์ค์ผ ์ ์๋ค.
What does jQuery.fn mean?
Why jQuery do this: jQuery.fn.init.prototype = jQuery.fn?
// jQuery-3.2.1.js - Code Line 2960
init = jQuery.fn.init = function( selector, context, root ) {
var match, elem;
// HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
}
// Handle HTML strings
if ( typeof selector === "string" ) {
// ... more code
}
// ...
return jQuery.makeArray( selector, this );
};// jQuery-3.2.1.js - Code Line 3060
init.prototype = jQuery.fn;jQuery.fn.init ์์ฑ์๋ jQuery.prototype ๊ฐ์ฒด์ selector๋ก ์ ๋ฌ๋ DOM ์์์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ์ฌ ๋ฐํํ๋ค. init ํจ์์ prototype ๊ฐ์ฒด๋ jQuery.fn ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ์ค์ ๋์ด ์๋ค.
์ฆ, ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ๊ฐ ํ์ฑ๋๋ค.

์๋ฅผ๋ค์ด, $("p")๋ฅผ ์คํํ๋ฉด jQuery.fn.init ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ฉฐ ๋ด๋ถ์๋ ๊ฒ์๋ DOM ์์๋ฅผ ๊ฐ์ง๊ฒ ๋๊ณ ,
init.prototype์ jQuery.fn๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก jQuery ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค. (ํ๋กํ ํ์
๊ธฐ๋ฐ์ ํจ์ ๊ณต์ )
์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฑ ๊ท์น์ ๋ชจ๋ ๊ฐ์ฒด๋ ์์ ์ ์์ฑํ ์์ฑ์ ํจ์์
prototypeํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋prototype๊ฐ์ฒด๋ฅผ ์์ ์__proto__ํ๋กํผํฐ๋ก ์ฐ๊ฒฐํ๋ค.

// IIFE๋ก jQuery ๋ณ์๋ฅผ $๋ก ๋ฐ์์ $.fn(jQuery.fn)์ redColorํจ์๋ฅผ ์ถ๊ฐํ๋ค.
(function($) {
$.fn.redColor = function() {
// this === jQuery.fn.init ๊ฐ์ฒด
this.each(function(index) {
// $(this) === DOM ์์ (p)
$(this).css("border", "4px solid #f00");
// $(this)๋ ๋ค์ ์๋ก์ด jQuery.fn.init ๊ฐ์ฒด๋ฅผ ์์ฑํด ๋ฐํํ๋ค.
// jQuery-3.2.1.js - Code Line 3041
/* HANDLE: $(DOMElement)
else if ( selector.nodeType ) {
this[ 0 ] = selector;
this.length = 1;
return this;
}
*/
})
// ๋ฉ์๋ ์ฒด์ธ๋ ํธ์ถ์ ์ํด this ๋ฆฌํด
return this;
}
})(jQuery);// jQuery.fn.each
// jQuery-3.2.1.js - Code Line 156
each: function( callback ) {
return jQuery.each( this, callback );
},
// jQuery.each
// jQuery-3.2.1.js - Code Line 356
each: function( obj, callback ) {
var length, i = 0;
if ( isArrayLike( obj ) ) {
length = obj.length;
for ( ; i < length; i++ ) {
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break;
}
}
} else {
for ( i in obj ) {
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break;
}
}
}
return obj;
},jQuery.fn.each()๋ฅผ ํธ์ถํ๋ฉด, this๋ฅผ ๋งค๊ฐ๋ณ์๋ก jQuery.each()๋ฅผ ํธ์ถํ๋ค.
jQuery ์์ค ์ฝ๋ ๋ถ์ 5. jQuery.fn.init
jQuery ์์ค์ฝ๋ ๋ถ์๊ณผ ํจ๊ปํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
$("p")์ ๊ฐ์ด ์ ํ์๋ฅผ ์ฌ์ฉํ๋ ค๊ณ ํ๋ ๊ฒฝ์ฐ jQuery.fn์ ๋ฉ์๋๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค.
์ ํ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ jQuery.prototype ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ jQuery.fn.init ์ธ์คํด์ค๊ฐ ์์ฑ๋๊ธฐ ๋๋ฌธ์
jQuery ๊ฐ์ฒด์ ์ถ๊ฐ๋ ๋ฉ์๋๋ ์ฌ์ฉํ ์ ์๋ค.
- ์ ํ์๋ฅผ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ jQuery.extend() ์ฌ์ฉ
- ์ ํ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ jQuery.fn.extend() ์ฌ์ฉ
jQuery.extend({
abc: function(){
alert('abc');
}
});
// usage: $.abc(). (No selector required like $.ajax().)
jQuery.fn.extend({
xyz: function(){
alert('xyz');
}
});
//usage: $('.selector').xyz(). (Selector required like $('#button').click().)jQuery : ํ๋ฌ๊ทธ์ธ ๊ฐ๋ฐ
Difference between jQuery.extend and jQuery.fn.extend?
A pure-JavaScript CSS selector engine
jQuery 1.3 ๋ฒ์ ๋ถํฐ ํฌํจ๋์ด ์ฌ์ฉ๋๊ณ ์๋ค.
// jQuery-3.2.1.js - Code Line 538
var Sizzle = (function( window ) {
function Sizzle( selector, context, results, seed ) {
//...
return result; // Array
}
//...
return Sizzle;
})( window );jQuery.find๋ก Sizzle ํจ์๋ฅผ ์ฐธ์กฐํ์ฌ, jQuery.fn.find๋ jQuery.fn.closet์ ๊ฐ์ด ์์ ํ์์ด ํ์ํ ๊ณณ์์ ์ฌ์ฉ๋๊ณ ์๋ค.
// jQuery-3.2.1.js - Code Line 2795
jQuery.find = Sizzle;
// jQuery-3.2.1.js - Code Line 2903
jQuery.fn.extend( {
find: function( selector ) {
var i, ret,
len = this.length,
self = this;
if ( typeof selector !== "string" ) {
return this.pushStack( jQuery( selector ).filter( function() {
for ( i = 0; i < len; i++ ) {
if ( jQuery.contains( self[ i ], this ) ) {
return true;
}
}
} ) );
}
ret = this.pushStack( [] );
for ( i = 0; i < len; i++ ) {
jQuery.find( selector, self[ i ], ret );
}
return len > 1 ? jQuery.uniqueSort( ret ) : ret;
},
})
// jQuery-3.2.1.js - Code Line 3091
jQuery.fn.extend( {
closest: function( selectors, context ) {
var cur,
i = 0,
l = this.length,
matched = [],
targets = typeof selectors !== "string" && jQuery( selectors );
// Positional selectors never match, since there's no _selection_ context
if ( !rneedsContext.test( selectors ) ) {
for ( ; i < l; i++ ) {
for ( cur = this[ i ]; cur && cur !== context; cur = cur.parentNode ) {
// Always skip document fragments
if ( cur.nodeType < 11 && ( targets ?
targets.index( cur ) > -1 :
// Don't pass non-elements to Sizzle
cur.nodeType === 1 &&
jQuery.find.matchesSelector( cur, selectors ) ) ) {
matched.push( cur );
break;
}
}
}
}
return this.pushStack( matched.length > 1 ? jQuery.uniqueSort( matched ) : matched );
},
})Sizzle
How jQuery selects elements using Sizzle
jQuery ํผํฌ๋จผ์ค ํฅ์์ ์ํ Tips And Tricks