Typescript中的TypeError

lee*_*eet 25 javascript typescript

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

未捕获的TypeError:对象原型可能只是一个Object或null:undefined

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));
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

actualLogicExpression.logicChildExpressions[0] = new LogicOr();
Run Code Online (Sandbox Code Playgroud)

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

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

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

小智 19

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

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

// File Parent.ts
export class Parent {
    public prop: string = "prop";
}
//File Child.ts
export class Child extends Parent {
    public prop2: string = "prop2";
}
Run Code Online (Sandbox Code Playgroud)

和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'));
});
Run Code Online (Sandbox Code Playgroud)

输出文件app.bundle.js将出错"Uncaught TypeError:Object prototype可能只是一个Object或null:undefined",因为生成的代码将首先执行Child类的创建(它依赖于Parent类)在加载父类之前.

如果您查看生成的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;
}());
Run Code Online (Sandbox Code Playgroud)

当你运行这个时,你会得到:

未捕获的TypeError:对象原型可能只是一个Object或null:setPrototypeOf()中未定义

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

return gulp.src(['code/Parent.js', 'code/Child.js'])
Run Code Online (Sandbox Code Playgroud)

有很多方法可以解决这个问题,这只是一个帮助您了解问题以及如何解决问题的示例.无论你找到什么方法来解决这个问题,最后,错误就是要求javascript引擎做一些你在执行时尚未给出指令的东西.

希望这会有所帮助,干杯


bas*_*rat 16

你在这一行得到一个错误:

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

错误消息是

未捕获的TypeError:对象原型可能只是一个Object或null:undefined

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

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

let Bar; 
class Foo extends Bar { } // Uncaught TypeError: Object prototype may only be an Object or null: undefined
Run Code Online (Sandbox Code Playgroud)

更多

修复bug LogicOr及其继承链.

  • 但是LogicOr从AbstractLogicExpression正确扩展。Typescript是否无法处理循环依赖项? (2认同)
  • “ Typescript是否无法处理循环依赖关系?”这是commonjs的局限性。建议删除它们。为了进行分析,我使用https://alm-tools.gitbooks.io/alm/content/features/dependency.html (2认同)
  • 我解决了循环依赖问题,现在可以工作了 (2认同)