A few questions about Javascripts Prototype -


i coming @ javascript classical oop background , having trouble understanding prototype.

given code example below:

  1. how call/execute bar in foo?
  2. why use "privileged" function instead of putting on prototype?
  3. this answered in q1 can bar1 , bar2 call each other?

    function foo()     {        this.property = "i'm property";         this.privileged = function()        {           // stuff        }     }      foo.prototype.bar = function()     {        // stuff     }      foo.prototype.bar2 = function()     {        // stuff     } 

there's lot of fud this day.

1). simple usage:

var xxx = new foo();  // create instance of object. xxx.privileged();  // calls internal closure. xxx.bar();  // first looks internals, looks prototype object. 

2). creates closure can modified on instance. not private @ (since can talk through object instance), rather individual copy of function, each instance of object gets new copy of function. can alter function itself, cannot alter globally. i'm not big fan of method of creating things.

3). yes:

foo.prototype.bar = function(){     this.bar2(); }  foo.prototype.bar2 = function(){     this.bar(); } // although... never both.  you'll wind in circular chain. 

for edification, built fiddle should show how things called: http://jsfiddle.net/7y5qk/


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -