Jump to content

reference to function


astralaaron

Recommended Posts

I just want to understand why when you reference to the function instead of calling it as function() { funcName(); } that the properties aren't initialized.

function myScript(){	this.ready = true;	this.test = function() { alert('Path ok & this.ready = ' + this.ready); };}var myScript= new myScript();  // calling the function this way runs the test function and outputs 'this.ready' as true//document.addEventListener("DOMContentLoaded", function() { myScript.test() }, false);  //this way runs the test function but outputs 'this.ready' as undefineddocument.addEventListener("DOMContentLoaded", myScript.test , false);

Are there any quick explanation or websites explaining this? One other question...I am reading one of O'Reilly's books and he suggested that you should define a methods of a class using 'prototype' and he refers to them as instance methods. I think that I understand but am not sure... He made it sound like if you don't use prototype to define methods and define them in the constructor class that each time you create a new class it defines those functions again. Does defining the methods with prototype make the script any faster? example:

function myScript(){	this.ready = true; }myScript.prototype.test = function() { alert( 'this.ready = ' + this.ready); }; var myScript= new myScript();document.addEventListener("DOMContentLoaded", function() { myScript.test() }, false);

Link to comment
Share on other sites

You're overwriting the function myScript() with myScript. Give the variable a different name:

var scr = new myScript();

I don't see a difference between using a prototype and defining it within the function itself. As far as I know it's not any faster.

Link to comment
Share on other sites

I suppose when you pass the function by reference it's running in the scope of the window. Scope problems are usually solved like this:

function myScript() {  var that = this; // From now on "that" refers to this particular object and not the scope of the function that's currently running.  this.ready = true;  this.test = function() { alert('Path ok & this.ready = ' + that.ready); };}

Link to comment
Share on other sites

That seems to do the trick and now that you mention that I remember reading something about this / that edit: since there is no benefit to prototype I will definitely not be doing it that way. Especially after trying to call a function I made with prototype from an init function that fires off on this event document.addEventListener("DOMContentLoaded", script.init, false); and the function seems to be non existent...sounds like another headache works fine when I just defined the function within the constructor

Link to comment
Share on other sites

I wouldn't say there's no benefit to prototype. maybe if you showed us your example we could figure out. scoping issues can usually be resolved in simple fashions like in Ingolme's example. Personally I try to avoid this within my functions/classes. Although Javascript is prototypical, a psuedo-classical implementation allows for the use of private/public members/methods (with closures), resolves some common scoping issues related to using this, and I guess I just kinda like the convention in general, especially since I tended to start making a lot more library code for my work as well as developing some API's. you could re-write Ingolme's like so

function myScript() {  var ready = true;   return {    test : function() {      alert('Path ok & this.ready = ' + ready);    }  }; }; var script = new myScript();script.test();

Link to comment
Share on other sites

No, that's fine. If you use "this" then the method or variable will be accessible from outside the function. If you just use var then it will be private and only functions belonging to the object can access the variable.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...