admin管理员组文章数量:1026495
In my application, third party game developers will write simple javascript logic based on my template. They will enter their code into a textbox (online), and I will store their code as Strings.
For example, one game developer would write this:
const examineItem = (user, item, app) => { //we will pass `user` and `item` with our own methods
//Developer writes his/her own logic
if(user.intelligence > 50){
return({ result: "You see a rock!"});
}else{
return({ result: "You see nothing"});
}
};
In my React Native app, how can I "convert" this string code into a runnable function? Do I need some interpreter? Do I use eval
(which is deprecated)?
Where do I get started?
In my application, third party game developers will write simple javascript logic based on my template. They will enter their code into a textbox (online), and I will store their code as Strings.
For example, one game developer would write this:
const examineItem = (user, item, app) => { //we will pass `user` and `item` with our own methods
//Developer writes his/her own logic
if(user.intelligence > 50){
return({ result: "You see a rock!"});
}else{
return({ result: "You see nothing"});
}
};
In my React Native app, how can I "convert" this string code into a runnable function? Do I need some interpreter? Do I use eval
(which is deprecated)?
Where do I get started?
Share Improve this question asked Jul 11, 2018 at 2:44 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 1- javascript.info/new-function – Isaac Commented Jul 11, 2018 at 3:04
2 Answers
Reset to default 5It's actually not too hard. Use the "new function" operator like so:
const examineItem = (user, item, app) => {
// Developer writes his/her own logic
const devString = 'return user.intelligence > 50'
// You can pass as many arguments as you want, just keep the string last:
const devFunc = new Function('user', 'item', 'app', devString)
// Make sure to pass all the arguments to the call:
if(devFunc(user, item, app)){
return({ result: "You see a rock!"});
} else{
return({ result: "You see nothing"});
}
};
// Testing it:
const user = { intelligence: 60 }
const res = examineItem(user, null, null)
console.log(res) // { result: "You see a rock!"}
I just tested this code, and it should work. You could pull the "devString" from wherever you wanted.
The main danger with this would just be to make sure that all of your devs know which parameters will be passed to the "string" that they write, as well as what the data model is for those parameters.
I made a similar project once, dont forget that custom user code can alter your variables if not properly encapsulated I highly remend you to read about IIFEs functions.
Ex. user.intelligence = 1000; // will alter all the user object
In my application, third party game developers will write simple javascript logic based on my template. They will enter their code into a textbox (online), and I will store their code as Strings.
For example, one game developer would write this:
const examineItem = (user, item, app) => { //we will pass `user` and `item` with our own methods
//Developer writes his/her own logic
if(user.intelligence > 50){
return({ result: "You see a rock!"});
}else{
return({ result: "You see nothing"});
}
};
In my React Native app, how can I "convert" this string code into a runnable function? Do I need some interpreter? Do I use eval
(which is deprecated)?
Where do I get started?
In my application, third party game developers will write simple javascript logic based on my template. They will enter their code into a textbox (online), and I will store their code as Strings.
For example, one game developer would write this:
const examineItem = (user, item, app) => { //we will pass `user` and `item` with our own methods
//Developer writes his/her own logic
if(user.intelligence > 50){
return({ result: "You see a rock!"});
}else{
return({ result: "You see nothing"});
}
};
In my React Native app, how can I "convert" this string code into a runnable function? Do I need some interpreter? Do I use eval
(which is deprecated)?
Where do I get started?
Share Improve this question asked Jul 11, 2018 at 2:44 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 1- javascript.info/new-function – Isaac Commented Jul 11, 2018 at 3:04
2 Answers
Reset to default 5It's actually not too hard. Use the "new function" operator like so:
const examineItem = (user, item, app) => {
// Developer writes his/her own logic
const devString = 'return user.intelligence > 50'
// You can pass as many arguments as you want, just keep the string last:
const devFunc = new Function('user', 'item', 'app', devString)
// Make sure to pass all the arguments to the call:
if(devFunc(user, item, app)){
return({ result: "You see a rock!"});
} else{
return({ result: "You see nothing"});
}
};
// Testing it:
const user = { intelligence: 60 }
const res = examineItem(user, null, null)
console.log(res) // { result: "You see a rock!"}
I just tested this code, and it should work. You could pull the "devString" from wherever you wanted.
The main danger with this would just be to make sure that all of your devs know which parameters will be passed to the "string" that they write, as well as what the data model is for those parameters.
I made a similar project once, dont forget that custom user code can alter your variables if not properly encapsulated I highly remend you to read about IIFEs functions.
Ex. user.intelligence = 1000; // will alter all the user object
本文标签: How can I run javascript logic from a string in React NativeStack Overflow
版权声明:本文标题:How can I run javascript logic from a string in React Native? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745624628a2159786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论