T
Tom Cole
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.getTypeForExtension = function(ext) {
for (var i = 0; i < this.types.length; i++) {
var type = this.types;
var extension = this.extensions[type];
if (containsExtension(ext, extension)) {
return type;
}
}
return null;
}
var containsExtension = function(value, array) {
for (var i = 0; i < array.length; i++) {
if (array.toLowerCase() == value.toLowerCase()) {
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 getTypeForExtension.
Is there anything "wrong" with this code? Would any experts out there
recommend changes (i.e. adding methods(functions) 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 getTypeForExtension
are visible and can be called by any Extensions "instance", but
containsExtension cannot:
i.e.
var extensionList = new Extensions();
extensionList.addType(type, extensions); //this is visible, no
problem...
extensionList.getTypeForExtension(".gif"); //this is visible, no
problem...
extensionList.containsExtension(".gif", extensions); //this is NOT
visible and throws an error that says
//
extensionList.containsExtension 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.getTypeForExtension = function(ext) {
for (var i = 0; i < this.types.length; i++) {
var type = this.types;
var extension = this.extensions[type];
if (containsExtension(ext, extension)) {
return type;
}
}
return null;
}
var containsExtension = function(value, array) {
for (var i = 0; i < array.length; i++) {
if (array.toLowerCase() == value.toLowerCase()) {
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 getTypeForExtension.
Is there anything "wrong" with this code? Would any experts out there
recommend changes (i.e. adding methods(functions) 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 getTypeForExtension
are visible and can be called by any Extensions "instance", but
containsExtension cannot:
i.e.
var extensionList = new Extensions();
extensionList.addType(type, extensions); //this is visible, no
problem...
extensionList.getTypeForExtension(".gif"); //this is visible, no
problem...
extensionList.containsExtension(".gif", extensions); //this is NOT
visible and throws an error that says
//
extensionList.containsExtension is not a function...