Thanks, TJ. Especially for the explanation and the pointers, that
this is not automatic clears up a lot.
On Jul 15, 5:41 am, T.J. Crowder t...@crowdersoftware.com wrote:
Hi,
Why does the function quotes (within the class) fail when the exact
same function dquote (outside the class) works fine.
The function's fine; what's happening is that you're trying to use an
undefined symbol (in `doc_name`, `quotes` is undefined). `this` is
*never* implicit in JavaScript the way it is in some other languages,
you have to write it explicitly.
You could fix it by adding this.:
doc_name: function() { return this.quotes( this.name ); }
...but since `quotes` doesn't need access to any of the instance
properties, there's no reason to put it on the object at all. I'd
probably use a closure instead and make it just a function within the
closure:
var Test_01 = (function() {
function quotes( strStr ) {
return \ + strStr + \;
}
return Class.create( {
initialize: function( strName ) {
this.name = String( strName );
},
doc_name: function() { return quotes( this.name ); }
});
})();
There, I've wrapped up your Class.create call inside an anonymous
function (called a scoping function) and immediately called the
function. `quotes` ends up being in scope for any code inside the
scoping function, but out of scope for any code outside of it. It
makes `quotes` similar to a private class method in class-based
systems, although the mechanism at work (closures) is rather more
powerful than that. More on closures
here:http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html
I'd also steer away from using anonymous functions other than as
scoping functions (all of the functions in your Test_01 class are
anonymous); more
here:http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html
HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / comwww.crowdersoftware.com
On Jul 14, 4:50 pm, Zortag zor...@rcn.com wrote:
I'm pretty new to both JavaScript and Prototype, but have been
programming for over 30 years, and so this one is causing me to tear
out what little hair I have left. Basically, I can't seem to figure
out what is wrong when I move a function that works outside of a class
into a class, it is a very simple function, just wrap a piece of text
in double-quotes. But when called from within a class, the script
blows up and dies. I'm using Firefox, Firebug does see that the class
has the item and that it is a function; but the script dies as if it
could not find the function. The JavaScript console remains empty.
Why does the function quotes (within the class) fail when the exact
same function dquote (outside the class) works fine. I figure it's
something simple but it has eluded me.
html
head
script src=../prototype.js type=text/javascript/script
/head
body
script type=text/javascript
var printp = function( strStr ) { document.write( p + strStr
+ /p ); };
var dquote = function( strStr ) { return \ + strStr +
\; };
printp( Begin Test! );
printp( Test 1: + dquote( 'test 1' ) );
var Test_01 = Class.create( {
initialize: function( strName ) {
this.name = String( strName );
},
quotes: function( strStr ) { return \ + strStr +
\; },
doc_name: function() { return quotes( this.name ); }
});
var test_02 = new Test_01( 'test' );
printp( Test 2: \ + test_02.name + \ );
printp( Test 3: + test_02.doc_name() );
printp( End Test! );
/script
/body
/html
--
You received this message because you are subscribed to the Google Groups
Prototype script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.