Javascript 打字稿中的类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44421810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:30:35  来源:igfitidea点击:

TypeError in Typescript

javascripttypescript

提问by leet

Here my problem: I get this error:

这是我的问题:我收到此错误:

Uncaught TypeError: Object prototype may only be an Object or null: undefined

未捕获的类型错误:对象原型可能只是一个对象或 null:未定义

export abstract class AbstractLogicExpression {
    protected _logicChildExpressions: AbstractLogicExpression[] = Array();
    protected _precedence = 0;
    protected _parsed = false;
    protected _expressionType = "";

    protected rightAssociative = false;

    public toDNF() {
        for (let i = 0; i < this.logicChildExpressions.length; i++) {
            let actualLogicExpression: AbstractLogicExpression = this.logicChildExpressions[i];

            if (actualLogicExpression._expressionType == "~") {

                let logicConjunction = actualLogicExpression.logicChildExpressions[0];

                let var1 = logicConjunction.logicChildExpressions[0];
                let var2 = logicConjunction.logicChildExpressions[1];

                if (logicConjunction._expressionType == "*") {
                    actualLogicExpression.logicChildExpressions[0] = new LogicOr();
                    //actualLogicExpression.logicChildExpressions[0].add(new LogicNeg(var1));
                    //actualLogicExpression.logicChildExpressions[0].add(new LogicNeg(var2));
                }
            }
        }
    }
}

I get this error because of the line before the two commented lines:

由于两条注释行之前的行,我收到此错误:

actualLogicExpression.logicChildExpressions[0] = new LogicOr();

I tested it by comment and uncomment the lines, because I get no line number in the error message.

我通过注释测试并取消注释行,因为我在错误消息中没有得到行号。

Does someone know what I can do? If you need a little more code. I can post something...

有人知道我能做什么吗?如果您需要更多代码。我可以发一些东西...

Here the code of LogicOr: https://pastebin.com/T28Zjbtb

这里是LogicOr的代码:https://pastebin.com/T28Zjbtb

采纳答案by basarat

You get an error on this line:

您在此行上收到错误消息:

actualLogicExpression.logicChildExpressions[0] = new LogicOr();

actualLogicExpression.logicChildExpressions[0] = new LogicOr();

The error message is

错误信息是

Uncaught TypeError: Object prototype may only be an Object or null: undefined

未捕获的类型错误:对象原型可能只是一个对象或 null:未定义

It is very easy to understand once you are familiar with Classes and how they work (https://basarat.gitbooks.io/typescript/docs/classes.html).

一旦您熟悉了类及其工作原理,就很容易理解 ( https://basarat.gitbooks.io/typescript/docs/classes.html)。

The error means that new LogicOris failing because LogicOris extending something that is undefined. Simple example:

该错误意味着new LogicOr失败,因为LogicOr正在扩展undefined. 简单的例子:

let Bar; 
class Foo extends Bar { } // Uncaught TypeError: Object prototype may only be an Object or null: undefined

More

更多的

Fix the bug in LogicOrand its inheritance chain.

修复错误LogicOr及其继承链。

回答by jared.g

To add to this, the actual problem here with the circular dependency is because one of resources that have not loaded before they are used. You will also get this error if your resources are loading out of order.

除此之外,循环依赖的实际问题是因为其中一种资源在使用之前尚未加载。如果您的资源加载顺序错误,您也会收到此错误。

Consider this example that uses gulp to compile:

考虑这个使用 gulp 编译的例子:

// File Parent.ts
export class Parent {
    public prop: string = "prop";
}
//File Child.ts
export class Child extends Parent {
    public prop2: string = "prop2";
}

and the gulp to compile

和 gulp 编译

gulp.task('compile-js', function () {
return gulp.src(['code/Child.js', 'code/Parent.js'])
    .pipe(sourcemaps.init())
    .pipe(concat('app.bundle.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('app/build'));
});

The output file app.bundle.js will error with "Uncaught TypeError: Object prototype may only be an Object or null: undefined" because the resulting code will first execute the creation of the Child class (which has the dependency on the Parent class) before the parent class has been loaded.

输出文件 app.bundle.js 会报错“Uncaught TypeError: Object prototype may only be an Object or null: undefined”,因为生成的代码将首先执行子类的创建(它依赖于父类)在加载父类之前。

If you look at the resulting javascript you will get:

如果您查看生成的 javascript,您将得到:

var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var Child = /** @class */ (function (_super) {
    __extends(Child, _super);
    function Child() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.prop2 = "prop2";
        return _this;
    }
    return Child;
}(Parent));
var Parent = /** @class */ (function () {
    function Parent() {
        this.prop = "prop";
    }
    return Parent;
}());

And when you run this you will get:

当你运行它时,你会得到:

Uncaught TypeError: Object prototype may only be an Object or null: undefined at setPrototypeOf ()

Uncaught TypeError: Object prototype may only be an Object or null: undefined at setPrototypeOf ()

To fix this, simply change the order of the resources in your gulp file or whatever method you are using to prepare or load the javascript for the page.

要解决此问题,只需更改 gulp 文件中资源的顺序,或者更改用于准备或加载页面 javascript 的任何方法。

return gulp.src(['code/Parent.js', 'code/Child.js'])

There are many ways that this can be dealt with, this is just an example to help you understand the problem and how you might fix it. Whichever way you find to fix the problem, in the end, the error is asking the javascript engine to do something you haven't yet given instructions for at the time of execution.

有很多方法可以解决这个问题,这只是一个帮助您理解问题以及如何解决它的示例。无论您找到哪种方式来解决问题,最终,错误都是要求 javascript 引擎做一些您在执行时尚未给出指令的事情。

Hope this helps, Cheers

希望这有帮助,干杯