Javascript 减少重复代码,尝试将函数分配给变量-不起作用
为了减少重复的代码,我尝试创建一个if语句来为一个变量分配一个函数或另一个,但这不起作用 我试着做的是Javascript 减少重复代码,尝试将函数分配给变量-不起作用,javascript,function,dry,Javascript,Function,Dry,为了减少重复的代码,我尝试创建一个if语句来为一个变量分配一个函数或另一个,但这不起作用 我试着做的是 const graphCall = (params['sfid'] === 'potential_engagements') ? this.engagementService.potentialsGraph() : this.engagementService.graph() 该语法本身不会引发错误,但是当我尝试使用 graphCall.then(animate => 。。。它不
const graphCall = (params['sfid'] === 'potential_engagements') ? this.engagementService.potentialsGraph() : this.engagementService.graph()
该语法本身不会引发错误,但是当我尝试使用
graphCall.then(animate =>
。。。它不起作用!
我是否遗漏了什么,我是否可以不分配函数,是否有其他类似或不同的方法来检查和删除重复的代码
我的代码:
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
this.engagementService
.potentialsGraph()
.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
} else {
this.engagementService
.graph(params['sfid'])
.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
}
如果你能帮忙,谢谢你 潜在的块有两件事是另一个块没有的:
this.engagementService.potentialEngagements = true; <------
this.engagementService
.potentialsGraph() <------
.then(animate => {
this.engagementService
.graph(params['sfid']) <------
.then(animate => {
并称之为:
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
handleGraphProm(this.engagementService.potentialsGraph());
} else {
handleGraphProm(this.engagementService..graph(params['sfid']));
}
潜在的
块有两件事是另一个块没有的:
this.engagementService.potentialEngagements = true; <------
this.engagementService
.potentialsGraph() <------
.then(animate => {
this.engagementService
.graph(params['sfid']) <------
.then(animate => {
并称之为:
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
handleGraphProm(this.engagementService.potentialsGraph());
} else {
handleGraphProm(this.engagementService..graph(params['sfid']));
}
它不工作的原因可能是因为您还有一个未在三元操作中分配的
this.engagementService.potentialEngagements=true
。将电势图
和图形
返回的表格
放入变量。然后调用然后对其调用:
let graphCall;
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
graphCall = this.engagementService.potentialsGraph()
} else {
graphCall = this.engagementService.graph(params['sfid'])
}
graphCall.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
它不工作的原因可能是因为您还有一个未在三元操作中分配的this.engagementService.potentialEngagements=true
。将电势图
和图形
返回的表格
放入变量。然后调用然后对其调用:
let graphCall;
if (params['sfid'] === 'potential_engagements') {
this.engagementService.potentialEngagements = true;
graphCall = this.engagementService.potentialsGraph()
} else {
graphCall = this.engagementService.graph(params['sfid'])
}
graphCall.then(animate => {
this.graph = new EngagementGraph(d3.select('.Engagement-GraphSVG'));
this.track();
this.engagementService.isGraph = true;
this.engagementService
.getAllProducts()
.then(() => this.downloading = false)
.catch(err => this.pdfError = true)
this.findForumContact();
this.updateDateLabel();
this.addMemberPicture();
this.setup(animate);
})
.catch(error => this.handleError(error));
首先找出错误发生的地方我想你的问题是在你打电话时承诺已经解决了。然后,你应该尝试将var分配给engagementService.potentialsGraph
,不带括号()
。这样,当您调用承诺时,它将被调用。只是为了澄清:graphCall=(params['sfid']='potential\u engagements')?this.engagementService.potentialsGraph:this.engagementService.graph
Hi@enf0rcer,问题是我需要将参数值传递给.graph函数Hi-adiga,这将如何影响.potentialsGrap h函数?它正在检查参数“params”值首先查找错误发生的位置我认为您的问题是在您调用时承诺已经解决。然后,您应该尝试将var分配给engagementService.potentialsGraph
,不带括号()
。这样,当您调用承诺时,它将被调用。只是为了澄清:graphCall=(params['sfid']='potential\u engagements')?this.engagementService.potentialsGraph:this.engagementService.graph
Hi@enf0rcer,问题是我需要将参数值传递给.graph函数Hi-adiga,这将如何影响.potentialsGrap h函数?它正在检查参数“params”值