The code section works just fine, but I want to see if
A) I'm writing this type of code correctly, and
B) I'm actually getting what I expect.
The code looks like this (it is for mapping file extensions to the
document types recognized by SAP's DMS):
var Extensions = function() {
this.types = new Array();
this.extensions = new Array();
this.addType = function(type, ext) {
this.types.push (type);
this.extensions[type] = ext;
}
this.getTypeFor Extension = function(ext) {
for (var i = 0; i < this.types.leng th; i++) {
var type = this.types[i];
var extension = this.extensions[type];
if (containsExtens ion(ext, extension)) {
return type;
}
}
return null;
}
var containsExtensi on = function(value, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].toLowerCase() == value.toLowerCa se()) {
return true;
}
}
return false;
}
}
What I do is create an Extensions variable, populate it through
addType and then later retrieve
the document type for a given extension via getTypeForExten sion.
Is there anything "wrong" with this code? Would any experts out there
recommend changes (i.e. adding methods(functio ns) to the prototype).
While the code I'm writing always appears to work, I have to admit
that with it's loose typing, I don't always know exactly what's going
on behind the scenes with the code. It just works the way I expect it
to, which I guess is better than the alternative :). I've heard the
term closure thrown around, but I have not been able to fully
understand the concept. I'm a java developer, so these things are a
little foreign to me.
From what I've written I expect that addType and getTypeForExten sion
are visible and can be called by any Extensions "instance", but
containsExtensi on cannot:
i.e.
var extensionList = new Extensions();
extensionList.a ddType(type, extensions); //this is visible, no
problem...
extensionList.g etTypeForExtens ion(".gif"); //this is visible, no
problem...
extensionList.c ontainsExtensio n(".gif", extensions); //this is NOT
visible and throws an error that says
//
extensionList.c ontainsExtensio n is not a function...
A) I'm writing this type of code correctly, and
B) I'm actually getting what I expect.
The code looks like this (it is for mapping file extensions to the
document types recognized by SAP's DMS):
var Extensions = function() {
this.types = new Array();
this.extensions = new Array();
this.addType = function(type, ext) {
this.types.push (type);
this.extensions[type] = ext;
}
this.getTypeFor Extension = function(ext) {
for (var i = 0; i < this.types.leng th; i++) {
var type = this.types[i];
var extension = this.extensions[type];
if (containsExtens ion(ext, extension)) {
return type;
}
}
return null;
}
var containsExtensi on = function(value, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].toLowerCase() == value.toLowerCa se()) {
return true;
}
}
return false;
}
}
What I do is create an Extensions variable, populate it through
addType and then later retrieve
the document type for a given extension via getTypeForExten sion.
Is there anything "wrong" with this code? Would any experts out there
recommend changes (i.e. adding methods(functio ns) to the prototype).
While the code I'm writing always appears to work, I have to admit
that with it's loose typing, I don't always know exactly what's going
on behind the scenes with the code. It just works the way I expect it
to, which I guess is better than the alternative :). I've heard the
term closure thrown around, but I have not been able to fully
understand the concept. I'm a java developer, so these things are a
little foreign to me.
From what I've written I expect that addType and getTypeForExten sion
are visible and can be called by any Extensions "instance", but
containsExtensi on cannot:
i.e.
var extensionList = new Extensions();
extensionList.a ddType(type, extensions); //this is visible, no
problem...
extensionList.g etTypeForExtens ion(".gif"); //this is visible, no
problem...
extensionList.c ontainsExtensio n(".gif", extensions); //this is NOT
visible and throws an error that says
//
extensionList.c ontainsExtensio n is not a function...
Comment