Javascript 打字脚本错误:A';超级';当类包含初始化属性时,调用必须是构造函数中的第一条语句
我的项目中有以下类型脚本错误。。让我分享一下 一个样本,这样你可以看到我在处理什么Javascript 打字脚本错误:A';超级';当类包含初始化属性时,调用必须是构造函数中的第一条语句,javascript,angularjs,backbone.js,typescript,Javascript,Angularjs,Backbone.js,Typescript,我的项目中有以下类型脚本错误。。让我分享一下 一个样本,这样你可以看到我在处理什么 module CoreWeb { export class Controller implements IController { public $q; public $rootScope; public $scope:ng.IScope; public $state:ng.ui.IStateService; public $translate:ng.translate.I
module CoreWeb {
export class Controller implements IController {
public $q;
public $rootScope;
public $scope:ng.IScope;
public $state:ng.ui.IStateService;
public $translate:ng.translate.ITranslateService;
public appEvents;
public commonValidationsService;
public defaultPagingOptions = {
currentPage: 1,
pageSize: 10,
totalServerItems: 0,
maxSize: 5
};
public detailModelName:string;
public filter:string;
public listModelName:string;
public mode;
public modelDataService;
public modelDefaultProperty:string;
public ngDialog;
public notificationsService;
public pagingOptions:IPagingOptions;
public selectionStatus:boolean;
public serviceCreateFunction:string;
public serviceGetAllCanceller:ng.IDeferred<any>;
public serviceGetAllFunction:string;
public serviceGetOneFunction:string;
public serviceUpdateFunction:string;
public showInactive:boolean;
public tableAction:number;
public tableActions:ITableAction[];
public titleDataFactory;
public validationOptions;
public validationRules;
public orderBy = null;
public orderType = null;
constructor(
$q:ng.IQService,
$rootScope,
$scope:ng.IScope,
$state,
$translate:ng.translate.ITranslateService,
appEvents,
commonValidationsService,
detailModelName:string,
listModelName:string,
modelDataService,
modelDefaultProperty:string,
ngDialog,
notificationsService,
serviceCreateFunction:string,
serviceGetAllFunction:string,
serviceGetOneFunction:string,
serviceUpdateFunction:string,
titleDataFactory
) {
this.$q = $q;
this.$rootScope = $rootScope;
this.$scope = $scope;
this.$state = $state;
this.$translate = $translate;
this.appEvents = appEvents;
this.commonValidationsService = commonValidationsService;
this.detailModelName = detailModelName;
this.listModelName = listModelName;
this.modelDataService = modelDataService;
this.modelDefaultProperty = modelDefaultProperty;
this.ngDialog = ngDialog;
this.notificationsService = notificationsService;
this.serviceCreateFunction = serviceCreateFunction;
this.serviceGetAllCanceller = $q.defer();
this.serviceGetAllFunction = serviceGetAllFunction;
this.serviceGetOneFunction = serviceGetOneFunction;
this.serviceUpdateFunction = serviceUpdateFunction;
this.titleDataFactory = titleDataFactory;
this.mode = $rootScope.modeEnum.none;
this.pagingOptions = this.defaultPagingOptions;
this.selectionStatus = false;
this.showInactive = false;
this.tableAction = null;
this.tableActions = [
{id: 1, name: "Activate"},
{id: 2, name: "Deactivate"}
];
this.validationOptions = {showErrors: commonValidationsService.modes.property, showNotification: true};
this.activate();
}
现在,如果我像上面那样在超级调用之前初始化
merchandisingConstants
。我在吞咽过程中出现以下错误,我的页面没有显示任何内容。当类包含初始化属性或具有参数属性时,super
调用必须是构造函数中的第一条语句。我已经尝试了所有我能想到的方法来修复这些错误,你知道我该怎么做吗?当你扩展一个类时,你的构造函数:
super()
declare var App: ng.IModule;
module CoreWeb {
export class EntityMasterController extends Controller {
private currenciesDataSet;
private entity: IEntityMasterModel;
private merchandisingConstants;
private typeAheadOptions;
constructor(
$q:ng.IQService,
$rootScope,
$scope:ng.IScope,
$state,
$translate:ng.translate.ITranslateService,
appEvents,
commonValidationsService,
entityDataService,
merchandisingConstants,
ngDialog,
notificationsService,
titleDataFactory
) {
// Must be first
super(
$q,
$rootScope,
$scope,
$state,
$translate,
appEvents,
commonValidationsService,
"entity",
null,
entityDataService,
"name",
ngDialog,
notificationsService,
"createEntity",
"getCurrentEntity",
"getEntity",
"updateEntity",
titleDataFactory
);
this.merchandisingConstants = merchandisingConstants;
}
这是一个相当复杂的问题,但它是解决该问题的一个简单方法:
super(
(this.merchandisingConstants = merchandisingConstants, $q),
$rootScope,
$scope,
$state,
$translate,
appEvents,
commonValidationsService,
"entity",
null,
entityDataService,
"name",
ngDialog,
notificationsService,
"createEntity",
"getCurrentEntity",
"getEntity",
"updateEntity",
titleDataFactory
);
它使用了一些奇怪且不常用的JavaScript,
操作符将赋值作为副作用塞进其中。你可以用任何一个参数,真的,但我用第一个参数。逗号运算符-与分隔函数参数的逗号不同,尽管它当然是完全相同的字符-允许您将表达式列表串在一起,所有表达式都将被计算。只有最后一个用作整个表达式的值
因此,通过这样做,您可以在计算参数列表时完成赋值。第一个参数的值仍将是
$q
,但赋值也将在调用super()函数之前进行
已生成。您始终可以通过逗号运算符或其他方式将初始化粘贴到super
参数列表中。将移动到super
调用下面的任务有什么问题?我认为从错误消息中可以明显看出,您只需要移动super(…
作为构造函数中的第一条语句。@我该怎么做?将super()
的第一个参数设置为(this.merchandisingConstants=merchandisingConstants,$q)
-这将产生在super()之前执行赋值的效果
调用发生,它也会像在代码中一样传递$q
的值。请确保包含括号,以便,
在参数列表中被解释为逗号运算符,而不是逗号分隔符。这会在我尝试使用该控制器时引发错误。如何在super之前初始化它?可以吗您描述了基类如何依赖于此属性?基类并不真正依赖于此属性,它是我需要在子类中使用的属性。但是,如果我在超级调用后初始化此属性并尝试在代码中使用它,我会遇到一个未定义的错误。您能提供一个示例来说明问题本身-mechandisegon吗stants
必须在某个地方使用,可能super
构造函数使用了依赖它的子类中重写的方法?这样就可以了,去掉了super错误。但是我现在得到了这个错误。'this'不能在当前位置引用@pointy@frontenddev嗯,根本的问题是TypeScript想要这个对象在子类“接触”它之前由父类初始化。这真正想告诉你的是(我不同意,不一定),你的对象层次结构有一些不纯的东西:需要你的子类在基类之前初始化是语言不喜欢的事情。
super(
(this.merchandisingConstants = merchandisingConstants, $q),
$rootScope,
$scope,
$state,
$translate,
appEvents,
commonValidationsService,
"entity",
null,
entityDataService,
"name",
ngDialog,
notificationsService,
"createEntity",
"getCurrentEntity",
"getEntity",
"updateEntity",
titleDataFactory
);