简体   繁体   中英

JavaScript: Function vs Variable

I am trying to store JavaScript function in the database. Every row in the database will have a different function. Depending on the row called a specific function will execute in the browser.

Some clarification: I am not trying to execute the function at the server. It will only be stored at the server and fetched to the client and would be executed like any other javascript function.

First, my question is, is this possible. I have been reading that javascript allows functions to be stored in variables, so I thought why not in the database. This will give me the freedom to execute a different function based on the row.

Second, I have been getting this error:

>a=cell._cell.row.data.jsfunction
"function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}"
>a()
VM435:1 Uncaught TypeError: a is not a function
    at eval (eval at cellClick (empRead.html:14), <anonymous>:1:1)
    at t.cellClick (empRead.html:21)
    at w.edit (tabulator.min.js:6)
    at HTMLDivElement.<anonymous> (tabulator.min.js:6)

It is a simple Fahrenheit to Celsius conversion function. I am afraid it is treating a variable as a string. But then how to tell it, I want it to be treated as a function. Is there any workaround to achieve it.

thanks

A variable is just a means to represent a value.

JavaScript supports lots of different kinds of value, including Numbers, Strings, Arrays and Functions.

To store something in a database, you have to express it as a type of value that the database understands. I'm not aware of any database that supports "JavaScript function" as a data type.

You need, instead, to store some kind of representation of the data in a format that the database does understand.

For example, the source code of the function, which you could then eval on the client (This appears to be what you are attempting, but you forgot the eval part). This opens up a whole can of worms and is not generally recommended.

Typically, the best approach is to have a bunch of predefined functions as part of the client-side JS program (possibly organised as properties of an object) and then store an identifier in the database (eg the property name).

You can then so something like:

my_trusted_functions[cell._cell.row.data.jsfunction]();

Probably not a good idea. In order to store a function correctly you need to store its entire declaration context which is obviously not desirable (if possible). What you could do, as suggested in other answers is creating a DSL for your app and store into the DB a representation of an expression in this DSL, hopefully a lot simpler than a JS AST.

For instance if the domain of your app is working with temperature data you could have some generic functions like convert(from, to, value) , add(value1, value2) wich are easily composable. Storing a representation of the convert function is just storing a map of matching between the different units, add is just a composition of convert with the addition.

My advice would be to check if:

  • your domain can be modeled with pure data, if it is the case then bingo, store this and compose it in JS.
  • your domain can be modeled with a DSL, see its complexity and maybe store some kind of representation of it if needed

and act accordingly.

Yes it is done. Help came from this post:

Given a string describing a Javascript function, convert it to a Javascript function

I used user984003 answer. Thanks a lot user984003.

Here is the solution:

>s = window.document.createElement("script");
>s.innerHTML = cell._cell.row.data.jsfunction
"function toCelsius() {
  return (5/9) * (200-32);
}"
>window.document.body.appendChild(s)
<script>​function toCelsius() {
  return (5/9) * (200-32);
}​</script>​
>toCelsius()
93.33333333333334

Thanks to everybody

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM