Why {} + {} is NaN only on the client side? Why not in Node.js?

Updated note: this has been fixed in Chrome 49. Very interesting question! Let’s dig in. The root cause The root of the difference is in how Node.js evaluates these statements vs. how the Chrome development tools do. What Node.js does Node.js uses the repl module for this. From the Node.js REPL source code: self.eval( ‘(‘ … Read more

How to print / echo environment variables?

These need to go as different commands e.g.: NAME=sam; echo “$NAME” NAME=sam && echo “$NAME” The expansion $NAME to empty string is done by the shell earlier, before running echo, so at the time the NAME variable is passed to the echo command’s environment, the expansion is already done (to null string). To get the … Read more

Why exactly is eval evil?

There are several reasons why one should not use EVAL. The main reason for beginners is: you don’t need it. Example (assuming Common Lisp): EVALuate an expression with different operators: (let ((ops ‘(+ *))) (dolist (op ops) (print (eval (list op 1 2 3))))) That’s better written as: (let ((ops ‘(+ *))) (dolist (op ops) … Read more

How to modify a global variable within a function in bash?

When you use a command substitution (i.e., the $(…) construct), you are creating a subshell. Subshells inherit variables from their parent shells, but this only works one way: A subshell cannot modify the environment of its parent shell. Your variableĀ e is set within a subshell, but not the parent shell. There are two ways to … Read more

Executing elements inserted with .innerHTML

Simplified ES6 version of @joshcomley’s answer with an example. No JQuery, No library, No eval, No DOM change, Just pure Javascript. http://plnkr.co/edit/MMegiu?p=preview var setInnerHTML = function(elm, html) { elm.innerHTML = html; Array.from(elm.querySelectorAll(“script”)).forEach( oldScript => { const newScript = document.createElement(“script”); Array.from(oldScript.attributes) .forEach( attr => newScript.setAttribute(attr.name, attr.value) ); newScript.appendChild(document.createTextNode(oldScript.innerHTML)); oldScript.parentNode.replaceChild(newScript, oldScript); }); } Usage $0.innerHTML = HTML; … Read more

Is there an eval() function in Java?

You can use the ScriptEngine class and evaluate it as a Javascript string. ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName(“js”); Object result = engine.eval(“4*5”); There may be a better way, but this one works.

Why is using ‘eval’ a bad practice?

Yes, using eval is a bad practice. Just to name a few reasons: There is almost always a better way to do it Very dangerous and insecure Makes debugging difficult Slow In your case you can use setattr instead: class Song: “””The class to store the details of each song””” attsToStore=(‘Name’, ‘Artist’, ‘Album’, ‘Genre’, ‘Location’) … Read more

Convert a string to a template string

In my project I’ve created something like this with ES6: String.prototype.interpolate = function(params) { const names = Object.keys(params); const vals = Object.values(params); return new Function(…names, `return \`${this}\`;`)(…vals); } const template=”Example text: ${text}”; const result = template.interpolate({ text: ‘Foo Boo’ }); console.log(result);

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)