了解评估参考 [英] Understanding Eval Reference
问题描述
我知道关于此主题还有其他话题,但我再次询问是因为我确实在努力理解评估以及如何正确使用它,以便它不会变成邪恶.通过查看此代码,如何获取console.log(this.arrTest);
记录['Back to Original']
?我尝试了许多变体,但没有一个起作用.
I know that there are other threads on this topic but I am asking again because I am really trying hard to understand eval and how to use it correctly so that it doesn't become evil. By looking at this code, how can I get the console.log(this.arrTest);
to log ['Back to Original']
? I tried many variations and none of them work.
我不想要此解决方案,请:this.TestClass.Run.call(this, "this.arrTest = ['Back to Original'];");
我要在课程级别内使用解决方案.
I do not want this solution please: this.TestClass.Run.call(this, "this.arrTest = ['Back to Original'];");
I want a solution inside the class level.
这里是一个有角的闪电战,可以玩: https://stackblitz.com/edit/angular-mtuvvz?file=src%2Fapp%2Fapp.component.ts
Here is an angular blitz to play with: https://stackblitz.com/edit/angular-mtuvvz?file=src%2Fapp%2Fapp.component.ts
export class AppComponent {
arrTest = ['Original'];
constructor() {
this.TestClass.Run("this.arrTest = ['Back to Original'];");
console.log(this.arrTest);
};
TestClass = {
Run : (pString) => {
this.arrTest = ['Changed']; //So this works
eval(pString); //Why is this not working
eval.call(this, pString); //Why is this not working
eval.call(null, pString); //Why is this not working
(1, eval)(pString); //Why is this not working
(eval)(pString); //Why is this not working
var my_eval = eval; //Why is this not working
my_eval(pString); //Why is this not working
}
};
}
我已经阅读了这两篇文章,但我不理解它们的解决方案:
I have read both of these articles but I don't understand a solution from them:
http://2ality.com/2014/01/eval.html
http://blog.klipse.tech /javascript/2016/06/20/js-eval-secrets.html
推荐答案
所以这在我的场景中很好地解决了这个问题:
So this solves the problem quite well in my scenario:
export class AppComponent {
arrTest = ['Original'];
constructor() {
this.TestClass.Run("arrTest", "['Back to Original']");
console.log(this.arrTest);
};
TestClass = {
Run : (pKey, pValue) => {
this.arrTest = ['Changed']; //So this works
this[pKey] = pValue;
}
};
}
这篇关于了解评估参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!