Skip to content

javascript_jquery_basic_4_1a

KyusungDev edited this page May 8, 2017 · 25 revisions

$(selector)๋Š” ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”๊ฐ€?

'$'๋ณ€์ˆ˜

// jQuery-3.2.1.js - Code Line 10246
window.jQuery = window.$ = jQuery;

jQuery์™€ $๋Š” ๋™์ผํ•œ ๋ณ€์ˆ˜๋‹ค

jQuery ์†Œ์Šค ์ฝ”๋“œ ๋ถ„์„ 2. ํ•จ์ˆ˜ ๊ฐ์ฒด $

'jQuery' ๋ณ€์ˆ˜

// 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.fn์€ jQuery.prototype์˜ ๋ณ„์นญ์ด๋‹ค. ์ด๊ฒƒ์€ ๋ฏธํ•™์ ์ด๋ฉฐ ์กฐ๊ธˆ๋งŒ ํƒ€์ดํ•‘์„ ํ•ด๋„๋œ๋‹ค.

What does jQuery.fn mean?
Why jQuery do this: jQuery.fn.init.prototype = jQuery.fn?

jQuery.fn.init ์ƒ์„ฑ์ž

// 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 ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ์„ค์ •๋˜์–ด ์žˆ๋‹ค.

์ฆ‰, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ํ˜•์„ฑ๋œ๋‹ค.
javascript_jquery_basic_4_1a_1

์ •๋ฆฌํ•˜๋ฉด, $("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.each()

// 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 ์†Œ์Šค์ฝ”๋“œ ๋ถ„์„๊ณผ ํ•จ๊ป˜ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ณต๋ถ€

jQuery ํ™•์žฅ vs. jQuery.fn ํ™•์žฅ

$("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?

Clone this wiki locally