Z
Z
ZaxapKramer2016-08-13 03:56:55
JavaScript
ZaxapKramer, 2016-08-13 03:56:55

How can you get away from using Function (new Function) in JavaScript by writing the same "normal function"?

I use Function when writing a template engine in order to replace text with logic , which is very convenient and makes the code short, but such code becomes poorly supported when the logic becomes more complicated.
We are talking about something similar, only with more complex logic (this is a very readable example):

var template = function(a) {
  return Function("o","o="+JSON.stringify(a).replace(/<%=(.+?)%>/g,'"+($1)+"').replace(/<%(.+?)%>/g,'";$1\no+="')+";return o")
};

Is it possible to rewrite it somehow? Any ideas?
Thanks in advance:)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-08-13
@ZaxapKramer

Function is used to "compile" an expression/functional, so you can directly execute the desired code, instead of "interpreting" it, which gives better performance and saves memory.
Yes, you can make a similar functional by storing data in closed variables, I interpret the functional, but it will work slower.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question