这是我的问题:
我收到此错误:
未捕获的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));
}
}
}
}
}
由于两条注释行之前的行,我收到此错误:
actualLogicExpression.logicChildExpressions[0] = new LogicOr();
我通过注释测试它并取消注释行,因为我在错误消息中没有得到行号。
有人知道我能做什么吗?
如果您需要更多代码。我可以张贴一些东西......
这里是LogicOr的代码:
https://pastebin.com/T28Zjbtb
你在这一行上收到错误:
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
更多
修复bug LogicOr
及其继承链。
除此之外,循环依赖的实际问题是因为在使用之前未加载的资源之一。如果您的资源无序加载,您也会收到此错误。
考虑使用gulp编译的这个示例:
// File Parent.ts
export class Parent {
public prop: string = "prop";
}
//File Child.ts
export class Child extends Parent {
public prop2: string = "prop2";
}
和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'));
});
输出文件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;
}());
当你运行这个时,你会得到:
未捕获的TypeError:对象原型可能只是一个Object或null:undefined
在setPrototypeOf()
要解决此问题,只需更改gulp文件中的资源顺序或用于准备或加载页面的javascript的任何方法。
return gulp.src(['code/Parent.js', 'code/Child.js'])
有很多方法可以解决这个问题,这只是一个帮助您了解问题以及如何解决问题的示例。无论你找到什么方法来解决这个问题,最后,错误就是要求javascript引擎做一些你在执行时尚未给出指令的东西。
希望这可以帮助,
干杯