[REQUEST] Template strings

Discussion and feedback on Construct 2

Post » Sat Aug 08, 2015 11:01 pm

[REQUEST] Template strings in the editor

@Ashley
This :
Code: Select all
Browser.ExecJS(`alert(${MY_VARIABLE})`)

instead of :
Code: Select all
Browser.ExecJS("alert('"&MY_VARIABLE&"')")


// CODE
Code: Select all
var interpolate = new Function("str","scope","tagFn",`
  /* The function constructor ignores strict mode
     allowing the 'with' statement for scoping
  */
  if(!scope){ if(tagFn) return (eval("tagFn " + str));
    else return (eval(str));
  } else with(scope){ if(tagFn) return eval("tagFn " + str);
    else return eval(str);
  };
`);


// TESTS
Code: Select all
var scope = { two:"2" };
console.log( interpolate("`${ parseInt(two) + 2 }`",scope) );
// [4]

console.log(
  interpolate("`${two}`",scope,
  function(strs,...vals){ // optional template function
      return strs.map(function(val,ind,arr) {
         if(vals[ind]){ return val + vals[ind];}
         else return val;
      }).join('');   
  })
);
// [2]
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sun Aug 09, 2015 12:42 pm

I wouldn't use this for Javascript code, that is always better in a custom plugin. What else would you use this for? Feature requests should have a comprehensive set of use cases to justify the possibly large amount of work that the feature would need.
Scirra Founder
B
395
S
231
G
88
Posts: 24,367
Reputation: 193,694

Post » Sun Aug 09, 2015 3:40 pm

Its worth noting template strings have access to eval `token:${ extJS() }`
Besides the js awesomeness ( which I hope you will consider );
  1. String concats
    `${myprop} : ${myvar}` vs ""&myprop&" : "&myvar
  2. Loading JSON *
    `{"prop":"val"}` vs "{""prop"":""val""}"

* I know you'll frown upon not loading assets via ajax request
but it adds an extra layer of asynchronous logic and it's not always convenient for small stuff you plan on storing in a variable to begin with.
B
11
S
2
G
1
Posts: 202
Reputation: 1,580


Return to Construct 2 General

Who is online

Users browsing this forum: mihirolover and 7 guests

cron