如何销毁在此构造函数中创建的对象?

时间:2022-06-12 03:50:22

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.

所以我正在制作一个可以用我的Java转换的JavaScript类。这一切都已完成,但我想确保为参数输入正确的数据类型。

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.

创建新的Table对象时,必须传入一个数组。虽然你可以在这里传递任何东西,但仍然会创建对象,只是没有标题和行字段。

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

我怎么能破坏这个物体?我试图抛出的错误没有做任何事情。

2 个解决方案

#1


You have 2 errors in your code.

您的代码中有2个错误。

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]。这应该是:

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.

如果抛出错误,则不会创建对象。

#2


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

(实际上,我倾向于将这个检查封装在一个单独的方法或方法中,以便更容易测试,并使ctor更具沟通性。)

#1


You have 2 errors in your code.

您的代码中有2个错误。

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]。这应该是:

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.

如果抛出错误,则不会创建对象。

#2


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

(实际上,我倾向于将这个检查封装在一个单独的方法或方法中,以便更容易测试,并使ctor更具沟通性。)