-3

I am very new to coding javascript plugin and i have in the last half year or so build about 2 plugins of my own, basically gallery plugins for my own personal use. now i usually use the following pattern in an IFFE.

$.Mypluginname = function() {
    this.anotherfunction(); // works !! 
}

$.mypluginname.prototype.anotherfunction(){
     // this points to $.Mypluginname awesome!!!
}

$.mypluginname.prototype.anotherfunction(){
    // this points to $.Mypluginname awesome!!!
}

But recently i have found a better JS plugin pattern, It looks like so,

$.Mypluginname = function() {

}

$.mypluginname.prototype = {

   anotherfunction : function(){
     // this points to $.Mypluginname awesome!!!  ... but how ? 
   }

   mypluginname : anotherfunction(){
    // this points to $.Mypluginname awesome!!!  ... but how ? 
   }

}

See how this still points to the main function, My question really is how ?

how is the this inside the below function:

$.Mypluginname = function() {
          this.anotherfunction(); // works !! ... can you answer this quinten.
    }

?

Can anybody explain ?

EDIT :: There are 12 answers in this thread. none of which answer my question , which was not "how does prototype work" in the first place. reading that thread will certainly enhance my knowledge of JS prototype , but i don't see how it would explain how this works in the below design pattern.

$.mypluginname.prototype = {

       anotherfunction : function(){
         // this points to $.Mypluginname awesome!!!  ... but how ? 
       }

       mypluginname : anotherfunction(){
        // this points to $.Mypluginname awesome!!!  ... but how ? 
       }

    } 

Thank you.

Alex-z.

Community
  • 1
  • 1
Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174

3 Answers3

1

It looks like you are asking how

$.mypluginname.prototype.anotherfunction = function() {
 // this points to $.Mypluginname awesome!!!
}

works the same as

$.mypluginname.prototype = {
   anotherfunction : function(){
     // this points to $.Mypluginname awesome!!!  ... but how ? 
   }
}

and the answer is that they are functionally the same; (one is replacing a key, one is replacing the whole prototype, but) in both cases you are setting anotherFunction on the prototype.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
0

The value of this depends on how you call a function, not how or where you define it. All the different variations of code you have in the question are about the definition of the function, not the calling of it, so they don't make any difference.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • i am sure your answer makes sense , but i am a JS newbie relatively , can you explain your answer , using the context i have provided . Thank would be more helpful ! TY. – Alexander Solonik Jul 29 '15 at 13:48
  • @AlexanderSolonik — As the answer says, none of the context you have provided is relevant to the value of `this`, so no, I can't. – Quentin Jul 29 '15 at 13:51
  • i have commented out a peice of of specially for you `this.anotherfunction(); // works !! ... can you answer this quinten.` , check it out and tell me if it still lacks context. – Alexander Solonik Jul 29 '15 at 13:58
0

this is always a reference to its Object. What you are creating is a constructor function. Javascript is a language which does inheritance with prototypes (prototypal inheritance). So once you have called your constructor function with the new operator like this:

var someObj = new myConstrFunction(param);

it will return you an Object with your variables and functions.

console.log(someObj)
//basicly returns something like this (simplyfied) 
//{anotherFunction:function(){},someVal:"",someOtherval:true};

these functions and variables can reference to their objects by using "this" in most cases.

Max Bumaye
  • 1,017
  • 10
  • 17