javascript - Usage of call(), in chris coyers example of custom events -


i going through chris coyers examples of custom events , come across following code :

$.fn.faq = function(options) {      return this.each(function(i, el) {        var base = el,               $base = $(el);            console.log(options);        base.init = function() {         // initialization stuff             $base                .find("dd")                .hide()                .end()                .find("dt")                .click(function() {                   var ans = $(this).next();                   if (ans.is(":visible")) {                    base.closeq(ans);                   } else {                    base.openq(ans);                   }                 })       };        base.openq = function(ans) {         // open panel                 ans.show();          // callback         options.qopen.call();       };        base.closeq = function(ans) {         // open panel                 ans.hide();          // callback         options.qclose.call();       };        base.init();      });  };  $("dl").faq({   qopen: myquestionopencallback,   qclose: myquestionclosecallback });  function myquestionopencallback() {   alert("answer opened!"); }  function myquestionclosecallback() {   alert("answer closed!"); } 

i refering chis coyers , perticular post :

custom event chris coyer

fiddle here

now question why in code javascript call() not being used set value of , why call being used ?? js practice ? or author choice , because if take off call on both below lines of code i.e. :

options.qopen.call();  options.qclose.call(); 

if changed

options.qopen();  options.qclose(); 

my plugin still works fine, why use of call() ??

i new js , jquery in general , please keep explanation simple .

thank you.

alexander .

it prevents functions passed callbacks options object modifying or accessing options object.

function myquestionopencallback() {   alert("answer opened!");   console.log(this); // window }  function myquestionclosecallback() {   alert("answer closed!");   console.log(this); // window } 

without .call(), this:

function myquestionopencallback() {   alert("answer opened!");   console.log(this); // {qopen: function, qclose: function} }  function myquestionclosecallback() {   alert("answer closed!");   console.log(this); // {qopen: function, qclose: function} } 

i see no reason (in specific case) since person using plugin has full access said options object.

var opts = {   qopen: myquestionopencallback,   qclose: myquestionclosecallback } $("dl").faq(opts); function myquestionopencallback() {   alert("answer opened!");   console.log(opts); // {qopen: function, qclose: function} }  function myquestionclosecallback() {   alert("answer closed!");   console.log(opts); // {qopen: function, qclose: function} } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -