Does this code snippet look okay?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Cole

    Does this code snippet look okay?

    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...

  • Tom de Neef

    #2
    Re: Does this code snippet look okay?

    "Tom Cole" wrote:
    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.
    >
    I am certainly not an expert, but my comments may be of help nevertheless.
    Basically, what you want is an aray with elements of the kind:
    knownTypes["gif"] = "picture"
    knownTypes["doc"] = "text"
    So, adding is via knownTypes[extension] = type;
    Retrieval via type = knownTypes[extension];
    Note that the addition will overwrite an existing entry for this extension.

    You can package this in an object with methods add and retrieve, like you
    have aimed to do.
    But what is the purpose of your array types ? The various types are already
    in the array extensions.
    I don't think you need to loop thru the types array in containsExtensi on.
    Just try the retrieval I mentioned.
    (In a loop like that I would always move the value.toLowerCa se() outside the
    loop for performance.)
    In getTypeForExten sion, extension = this.extensions[type] is (most likely)
    just a string. But you pass it as an array argument to
    containsExtensi on(ext, extension). I don't understand that.
    Why do you use var Extensions = function() ... instead of function
    Extensions() ?
    Same with ContainsExtensi on.

    HTH
    Tom


    Comment

    Working...