javascript help

Example simplified/contrived for this question. Recently, I came across following code sample.
There is a Js helper file defined as function() JsHelper and there are bunch of other methods created and defined
as JsHelper.prototype.... .

In the view code under document ready function, a new helper is created --> helper = new JsHelper();

My understanding is JsHelper object is created (even though , it is a function but can be used as a object).
and when once created it will execute all the code within the JsFile, is this right?

and also, in the JsHelper function, a variable me is assigned to this? how does it work?

-----------razor view code ------------------
       $(document).ready(function () {
            helper = new JsHelper();

----------JsFile ---------------------
function JsHelper(){
    var me = this;
     meElements = $("#form").find("input");
      me.dosomething ();

JsHelper.prototype.dosomething = function (){
     var me = this;
       $("#div").each( function () {
             //do sth with the elements
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

greetings  novice programmer, , , ,  There are several things to be aware of for what you ask, one is that most variables in JS are Objects, for instance arrays are also JS Objects.

functions in JS are Variables and have the Object properties and methods, so using -
var helper = new JsHelper;
will produce a new variable helper as an object is created ; This is just how javascript works, unlike many other languages.

AND YES, when once created it will execute all the code within the JsHelper() function that is in a place to be used at that time (but there can be code that is not done then, because you can have functions within this function)

you can have many variables inside a function and then there is the -  this  - which assigns properties and methods (or references those to use in the code)

so the line -
   var me = this;

is a way to use the    this  in places that can not use   this  like AJAX callback functions, you can use   me   there instead.

It is not real easy to understand the javascript   this    and it's "Scope" and how and when to use it. but for more complex code work the internal self reference of    this  can be very useful.

hope this is information you can use? ?
mikhaAuthor Commented:
@Slick812- thanks .

based on your answer, I understand functions in javascript are variables but they act like objects, where we can access their properties and
called methods on it?

also, in my sample above ,

what is this referring to in say function JsHelper(){....}
and if JsHelper.prototype.dosomething = function (){..} is called from JsHelper , what is this when dosomething function executes ?
?? ? you say -  "act like objects, where we can access their properties", , ? ? you access them like all the other object as you do in your code here -
     me.dosomething ();
see the period .  

the   me  s the object Variable, and to use props or methos just have the code "period"    .      after the object like this -
    var helper = new JsHelper();
    var number1 = helper.first;
    var array1 = helper.listCars("ford");

you ask - "what is this referring to in say function JsHelper(){....}"
  I tried to tell you that before, but assumed you knew something, sorry,   so Code that's inside of a function NEEDS to be able to assign props and methos to that INSTANCE when the object is made with the "new" word -
          var helper1 = new JsHelper();

the   this   is an internal "reference" to the produced Object props and methos,   this can be used (referenced) within the function to have a way to get (use) outside properties and methos that "Belong" to an INSTANCE object, and also to change several things linked to the object with all of it's props amd methos.

you can make another INSTANCE of the function class with a second or third   new
          var helper2 = new JsHelper();
          var helper3 = new JsHelper();
and now you have 3 variable objects from the one function class   JsHelper()  BUT the properties and internal variable "Values" can be different in each separate JsHelper variable Object INSTANCE  helper1,  helper2,  helper3
    if you add in code a property to just helper1 like -
       helper1.nonsence = "junk paint apples";

the helper2, and helper3 will NOT have that .nonsence property, and if you use it for helper2, you get undefined
   var ns1 = helper1.nonsence; // returns   "junk paint apples"
   var ns2 = helper2.nonsence; // returns   undefined

place two properties in function with  this
function JsHelper(){
this.feet = 2;
this.sound = "Beep";

make two instances -
          var helper1 = new JsHelper();
          var helper2 = new JsHelper();

change sound in first instance -
         helper1.sound = "AAAAAAAHHHH";

get the two instances   .sound
      var snd1 = helper1.sound;   // returns   "AAAAAAAHHHH"
   var snd2 = helper2.sound;  // returns   "Beep"

I am unsure what to answer to this -
    "what is this when dosomething function executes "
the browser is executing the code, so it does not matter, BUT the   this   as a code reference can not be used without a property or method, , EXCEPT to assign a variable as you did
    var me = this;
they use the  word  "me" as a name, because it refers to it's self the  JsHelper()  function code so the  this,  is ME the function that has it inside.

There is very much to javascript and understanding it,  so I am not going for more here.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.