0

I was reading through How to Make a Javascript Library and I came across a point where the author calls:

    function _() {
        //Some obects and variables and junk. . .
}

_.prototype = {
    //some code. . .
    myFunction: function() {
        //Bla bla bla. . .
    }
}

And I was wondering how this works, and what it does. I understand that it creates a command of _.myFunction() but I don't understand how. I was wondering if it is the only way, and if it requires some other globals included somewhere.

Thanks in Advance!

Edit: experimenting with how this works, I have discovered the following:

function _$() {
    //Bla bla bla. . . 
}
_$.prototype {
    myFunc: function(foo) {
        return foo;
    }
}

Then, when I call _$.myFunc I get: Unkown Syntax error: myFunc is not a function Just as Felix King said, it's not available. Could anyone tell me why, and how to make the function I set of myFunc accessible with _$.myFunc(null);

Travis
  • 1,274
  • 1
  • 16
  • 33
  • Presumably there is a piece above that that goes something along the lines of `var _ = function(){` or `function _(){`? – Jesse Kernaghan Jan 17 '15 at 04:23
  • @JesseKernaghan Yeah there is, I'll add that to the question. –  Jan 17 '15 at 04:26
  • You are mistaken. Assigning to `_.prototype.func` does not make the function available as `_.func`. And the tutorial you linked to doesn't claim that either. Nor does it set `_.prototype` inside the function itself. – Felix Kling Jan 17 '15 at 04:35
  • @FelixKling Where did I say I assigned _.prototype.func? And also, thanks for pointing out that it isn't in the function, I'll update it now. –  Jan 17 '15 at 04:41
  • 1
    @wyatt the .prototype functionality is a core part of how javascript works. I know you came here looking for solutions, but there are countless in-depth articles that will help you that already exist ( like http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work ). I suggest searching and reading more resources before posting. – Jesse Kernaghan Jan 17 '15 at 04:46
  • *"Where did I say I assigned _.prototype.func?"* `_.prototype = { myFunction: function() { }}` assigns a function to `_.prototype.myFunction`. – Felix Kling Jan 17 '15 at 05:43
  • If you want to know how `Func.prototype` works, I recommend to read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript . – Felix Kling Jan 17 '15 at 05:44
  • @FelixKling I'll go check out that article, and thanks for the tip on `.prototype.myFunction` –  Jan 17 '15 at 13:58

3 Answers3

2

Here the _.prototype property for the object _ is being modified. You can read more about prototype modification/method addition here.

Adding properties or methods to the prototype property of an object class makes those items immediately available to all objects of that class, even if those objects were created before the prototype property was modified.

nitishagar
  • 9,038
  • 3
  • 28
  • 40
1

I have what I was trying to do figured out. Prototype is simply an object type inside an object, refer to this.

The second part, and what I was trying to do was set something accessable by _$.function(args) The way to do this, discovered through expierements is:

var _$ = function() {
    //args and variables etc. . .
};
_$.myFunc = function(args) {
    return args;
}

Then, _$.myFunc(5) returns: 5

Community
  • 1
  • 1
Travis
  • 1,274
  • 1
  • 16
  • 33
0

Though, question has been answered a year ago, but I think it worth sharing so that it may help reader.

function _() {
        //Some obects and variables and junk. . .
        // missing like was 
        return this;

}

_.prototype = {
    //some code. . .
    myFunction: function() {
        //Bla bla bla. . .
    }
}

Only confusion in above code is '_' , since naming guideline of javascript says "any variable or function can start with _".If I we write this code as follows :

 function Blah() {
            //Some obects and variables and junk. . .
            return this;
    }

  Blah.prototype = {
        //some code. . .
        myFunction: function() {
            //Bla bla bla. . .
        }
    }

Now I think everybody can use or understand how to use it. like Joke().myFunction().

So I think the confusion was '_' here.

Anupam Singh
  • 1,158
  • 13
  • 25