Subj : Re: Problem using JS Objects and JS Objects implementing Java Interfaces To : Alvaro Diaz From : Igor Bukanov Date : Wed Apr 30 2003 04:33 pm alvaro.diaz@roxes.com wrote: > ... > i have found a working solution for my problem: > > function Directory( path) > { > this.filenameFilter = new java.io.FilenameFilter() > { > directories:[], > files:[], > > accept:function( dir, name) > { > if( new java.io.File( dir, name).isDirectory()) > this.directories.push( [name, new > Directory( path + "/" + name)]); > else > this.files.push( name); > } > } > > new java.io.File( path).list( this.filenameFilter); > > this.directories = this.filenameFilter.delegee.directories; > this.directories.sort(); > this.files = this.filenameFilter.delegee.files; > this.files.sort(); > > delete this.filenameFilter; > } > > var root = new Directory( "."); > > but its an ugly one. do you know an more elegant solution ? > especially copying from the delegee's seems to me really bad. > > my hardest problem is probably the scoping. > > if you know how to code it better please send it to me. This is due to a very bad JS design where nested functions will have as their this this original scope object, not this for parent function that calls them. A simple workaround would be to add something like var new_object = this to Directory and use it instead of this everywhere: function Directory( path) { var new_object = this; new_object.directories = []; new_object.files = []; var filenameFilter = new java.io.FilenameFilter() { accept:function( dir, name) { if( new java.io.File( dir, name).isDirectory()) new_object.directories.push( [name, new Directory( path + "/" + name)]); else new_object.files.push( name); } } new java.io.File( path).list( filenameFilter); new_object.directories.sort(); new_object.files.sort(); } var root = new Directory( "."); Regards, Igor .