간단한 js 엔진이 필요합니다. 그래서 함수를 만들었습니다 :JS 콘텐츠로 평가할 개체를 설정하는 방법은 무엇입니까?
function compile(tpl, scope){
return tpl.replace(/\{\{([\s\S]+?)\}\}/g, function(caught, content){
var compiled;
try{
compiled = eval.call(scope, content);
}catch(e){
compiled = caught;
console.error(e);
}finally{
return compiled;
}
});
}
좋아, 호출하십시오.
compile('<div>{{ maxSize/1024/1024 }}M</div>', { maxSize: 1048576 });
그러나, "정의되지 않은이 maxSize"밖으로 오류 점프.
어떻게 해결할 수 있습니까? 그러나 그것은 안전하지 않습니다,
try {
compiled = (new Function('with(this){return ' + content + '}')).call(scope);
} catch (...)
이 당신의 접근 방식의 작업을 할 것입니다 : 아마 도움이 될 수 있습니다 무엇
. 'eval'은 그 엔 클로징 스코프를 취할 것입니다. –
도움이 될 것입니다 : http://perfectionkills.com/global-eval-what-are-the-options/ – Sarfraz