0

Ok, so I'm a complete newbie to OOP in Javascript, apparently. I thought I understood it, but it appears I only know a small portion. Anyway, what I'm trying to do is setup an object to store and return data from an XML input by using a fairly simple string to retrieve data. I'd like to retrieve the data with a string similar to reader.getItem().getSubItem() or something like that.

Below is an example of what I attempted, but I get the error anonymous is not a function each time I try to do a call to fr.getType().isTexture() so obviously, I need to change something.

//Create the object by passing an XML element containing sub-elements
var fr = new FeatureReader(test.child(i));

alert(fr.getName()); //returns the object's name
alert(fr.getType().isTexture()); //"anonymous is not a function" error

function FeatureReader(feature) {
    var feat = feature;
    this.getName = function() {
        return feat.name;
    };
    this.getType = new function() {
        this.isTexture = new function() {
            if (feat.type.texture == "yes") {
                return true;
            }
            return false;
        };
        this.isModel = new function() {
            if (feat.type.model == "yes") {
                return true;
            }
            return false;
        };
    };
}

Now, obviously I could just remove the surrounding this.getType = function() {} around the this.isTexture and this.isModel to get my data, but for the sake of learning something, I'd like to see how it is recommended that I set this object up to get the returned values using a string similar to what I mentioned in the first and second paragraphs.

DGolberg
  • 2,109
  • 4
  • 23
  • 31

2 Answers2

2

When you do this:

    this.isTexture = new function() {
        if (feat.type.texture == "yes") {
            return true;
        }
        return false;
    };

you're setting the "isTexture" property to the object constructed, not to that function. If you drop the new keyword from the statement, you'll be setting "isTexture" to be a function.

An expression of the form new <some-function> evaluates to an object, in other words.

edit — your "getType" property will also be an object, for the same reason. However, I think this would work:

alert( fr.getType.isTexture() );

Also note that your if statement can be simplified:

  return feat.type.texture == "yes";
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Ok, but now I get `undefined is not an object` when attempting to access `isTexture()` – DGolberg Apr 14 '14 at 18:56
  • @DGolberg ah well "getType" will *also* be an object; I missed the function call in the alert calls. – Pointy Apr 14 '14 at 19:00
  • @DGolberg I think you're complicating things a bit though, why not simply assign an object? See https://gist.github.com/elclanrs/8ef4012a4ea3f5b7213e – elclanrs Apr 14 '14 at 19:02
  • @elclanrs That does work nicely, but is there a way to do the if-statement-based conversion from string to boolean? Also, could you make that an answer? That's pretty much what I was looking for. – DGolberg Apr 14 '14 at 19:14
0

What you can do is simply assign an object instead of using new:

function FeatureReader(feature) {
    var feat = feature;
    this.getName = function() {
        return feat.name;
    };
    this.getType = {
        isTexture: function() {
            return feat.type.texture == "yes";
        },
        isModel: function() {
            return feat.type.model == "yes";
        }
    };
}

Then use the method like:

instance.getType.isTexture()

Note that you don't need to return true or false, as returning an expression that evaluates to boolean like a == b returns a boolean value.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Ahh, did not catch the == at first. Very nicely done, and answers my question perfectly, and with less code than I was expecting! Thank you very much! – DGolberg Apr 14 '14 at 19:39
  • You'll want to remove those semicolons in the internal object declaration – Sam Dufel Apr 14 '14 at 20:04
  • Yeah, I use Eclipse to write the initial code and it pointed that one out to me. This is actually being used in Photoshop, but considering how unstable ESTK is, I've started writing my code in Eclipse first, then debugging in ESTK. Thanks for pointing that out, though, others may have missed it. – DGolberg Apr 15 '14 at 22:59