0

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.

nicael
  • 18,550
  • 13
  • 57
  • 90
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 1
    You can see [this SO question](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) and [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype) for prototype, [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get) for getter. Most generally, I advise you to take a look at [the MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) for learing how to use objects in Javascript. – Serge K. Feb 28 '14 at 16:20
  • The first just creates an object with a `get` property that has a function as its value. The `prototype` is JavaScript's mechanism of inheritance. These are fundamental questions. Probably a good idea to learn the language basics before trying to create a library. – cookie monster Feb 28 '14 at 16:20
  • 3
    Regarding the first question: [Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Using_object_initializers). Regarding `prototype` [Inheritance and the prototype chain](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain) – Felix Kling Feb 28 '14 at 16:21

1 Answers1

0

Okay!

So, when I asked this question I was a JavaScript newbie, and woefully unaware of objects!

Now that I know what an object is though, I recognise the syntax within the code and am able to understand what it means!

var DynamicScript = {
  get: function(){ 
    ...

Is just creating a property, get, with value function(){....

Dyn.prototype.map = function (callback) {

Does almost exactly the same thing, creates a property for Dyn called map, with a value of that function.

Ta-da!

theonlygusti
  • 11,032
  • 11
  • 64
  • 119