javascript - Why does jQuery store an element as a function? -
javascript - Why does jQuery store an element as a function? -
i calling div id of auto this:
var auto = $('#car'); console.log(car);
when log it, comes in console:
[div#car, context: document, selector: "#car", jquery: "1.9.0", constructor: function, init: function…]
is "constructor: function" part of log telling me has been stored function, , if why , how store object?
jquery creates instances of functions when accepts argument. in case, sent in selector id of element. upon receiving selector, jquery uses new
keyword on "internal" jquery function. result function object. every time utilize $
or jquery
asking jquery object result of new
beingness issued jquery function. object has many things attached it, including element array matching selector, , jquery's prototype allows access api.
so, clarify. object. constructed function.
edithere sample code help see how can happen.
first, demo simple jquery object: http://jsfiddle.net/pbdct/
html
<div id="car"></div>
js
var auto = $("#car"); console.log(car);
second, illustration wrote
html
<div id="car"></div>
js
var $ = (function(){ var $ = function(sel){ if( !(this instanceof $) ){ homecoming new $(sel); } this.selector = sel; this.element = document.getelementbyid(sel); this.constructor = $; this.init = function(){ /*init*/ }; }; homecoming $; })(); var auto = $("car"); console.log(car);
you notice both log exact same thing. there no magic in $
, string, much y
or jquery
, or helloworld
.
javascript jquery
Comments
Post a Comment