I am attempting to build my own JavaScript library using Andrew Burgess' online tutorial (http://code.tutsplus.com/tutorials/build-your-first-javascript-library--net-26796) and am following it fine, but I would like to know what the get: function(selector)
does in the following code:
(function() {
function Dyn(elems) {
for (var i; i < elems.length; i++) {
this[i] = elems[i];
}
this.length = elems.length;
}
var DynamicScript = {
/*here it is!-->*/get: function(selector) {
var elems;
if (typeof selector === "string") {
elems = document.querySelectorAll(selector);
} else if (selector.length) {
elems = selector;
} else {
elems = [selector];
}
return new Dyn(elems);
}
};
return DynamicScript;
}());
If anyone could tell me what it does I would be extremely grateful.
Also, in the tutorial, there is a function that looks like this:
Dyn.prototype.map = function (callback) {
var results = [], i = 0;
for ( ; i < this.length; i++) {
results.push(callback.call(this, this[i], i));
}
return results;
};
I would really like a nice, simple explanation of what .prototype
does. I haven't been able to understand what I have already come across about it, so if you could pretend you are talking to an idiot (not too much point in pretending) and explain it in the simplest terms possible I would appreciate it greatly.
Thanks for paying attention, I really do need the help.