0

So I'm making a JavaScript class that will be transferable with my Java one. It's all done but I want to make sure the right data type gets entered for the arguments.

For example, my Constructor:

function Table(header) {
    if (!Object.prototype.toString.call(header) === '[object Array]') {
        throw 'headers not array';
        return;
    }
    for (var i = 0; i < header.length; i++) {
        if (!typeof(header[i]) == 'string') {
            throw 'headers['+i+'] not string';
            return;
        }
    }
    this.header = header;
    this.rows = [];
}

When you create a new Table object, you have to pass in an array. Though you can pass in anything here and the object still gets created, just without the header and rows fields.

How can I destroy the object? The errors I've tried to throw don't do anything.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • I know this doesn't answer your question, but you can do `header instanceof Array` to check if it is an array. – forgivenson May 06 '15 at 17:02
  • 1
    Why do you need to "destroy" it? – Dave Newton May 06 '15 at 17:02
  • 2
    Javascript uses automatic garbage collection, you don't have to destroy objects by hand. When you stop referring to the object, it will eventually go away. – Barmar May 06 '15 at 17:03
  • @DaveNewton I think the intent is to cause the creation to fail if the correct parameter isn't passed in. – forgivenson May 06 '15 at 17:03
  • @DaveNewton if an array isn't passed in, the object is useless. If I can throw an error, it's better than the person using this twiddling their thumbs trying to work out what went wrong. – Spedwards May 06 '15 at 17:03
  • 1
    What do you mean by the "errors you've tried to throw don't do anything"? I mean, GC "just happens" in JS, and destroying the object from within itself seems contradictory. – Dave Newton May 06 '15 at 17:04
  • When you throw the exception, the object will be destroyed. – Barmar May 06 '15 at 17:05
  • 1
    If you're going to throw exceptions, you might as well throw `Error` objects: `throw new Error("whatever");` – Pointy May 06 '15 at 17:06
  • @DaveNewton I mean, if I run `var a = new Table('not an array');`, no errors are thrown and an invalid object is created. – Spedwards May 06 '15 at 17:07
  • possible duplicate of [How do you check if a variable is an array in JavaScript?](http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript) – Etheryte May 06 '15 at 17:09
  • @Spedwards Well... the way you've written it is broken; check your first logical condition in the console, and consider operator precedence, and consider something like `!==` instead. We have great JS development tools, like debuggers, the console, even simple console logging--might as well use them. – Dave Newton May 06 '15 at 17:09

2 Answers2

2

You have 2 errors in your code.

1)

!Object.prototype.toString.call(header) === '[object Array]'

!Object.prototype.toString.call(header) returns a boolean, so it's never going to equate to [object Array]. This should be:

Object.prototype.toString.call(header) !== '[object Array]'

2)

!typeof(header[i]) == 'string'

the same as above. This should be:

typeof header[i] !== 'string'

The object won't get created if the errors are thrown.

dewd
  • 4,380
  • 3
  • 29
  • 43
  • I ended up fixing these. It was from old recycled code from 2 years ago. They worked for what I was doing then. – Spedwards May 06 '15 at 18:08
0

Your first logic statement isn't doing what you think it is:

if (!Object.prototype.toString.call(header) === '[object Array]') {

The ! is operating on the call results, not the entire expression. Either:

if (!(Object.prototype.toString.call(header) === '[object Array]')) {

or

if (Object.prototype.toString.call(header) !== '[object Array]') {

will work much better. (Same with your second expression.)

Personally I'd lean towards the latter.

(Actually, I'd lean towards encapsulating this check in a separate method or methods, to make it easier to test, and to make the ctor more communicative.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • The former is what I had in my old code. When I re-wrote the code looking at the old one, I left out the brackets. I ended up changing to the latter not long before I checked for answers. – Spedwards May 06 '15 at 18:09