-
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__ํ๋กํผํฐ๋ก ์ฐ๊ฒฐํ๋ค.
// redColor() ๋์ ๊ณผ์
(function($) {
$.fn.redColor = function() {
// this === jQuery.fn.init ๊ฐ์ฒด
this.each(function(index) {
// this === DOM ์์ (p)
// $(DOM Element) ํํ๋ก ๋ค์ ๊ฒ์
// jQuery-3.2.1.js - Code Line 3041
// HANDLE: $(DOMElement) {...} ๋ถ๋ถ์์ ํด๋น DOM ์์ํฌํจํ jQuery.fn.init ๊ฐ์ฒด๋ฅผ ๋ฐํ
$(this).css("border", "4px solid #f00");
})
// ๋ฉ์๋ ์ฒด์ธ๋ ํธ์ถ์ ์ํด this ๋ฆฌํด
return this;
}
})(jQuery);redColor() ์์ ๋ค์๋ณด๊ธฐ
// 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 ) {
// obj[i] === this
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?