js multiline string的幾種做法

var str1 = "\
  function add1(a,b){\
    return a+b;\
  }\
  console.log(add1(1,2));\
";

var str2 = [
  'function add2(a,b){',
  '  return a+b;',
  '}',
  'console.log(add2(1,2));'
].join('\n');

var str3 = 'function add3(a,b){' +
           '  return a+b;'+
           '}'+
           'console.log(add3(1,2));';

var str4 = ` //ES6
  function add4(a,b){
    return a+b;
  }
  console.log(add4(1,2));
`;

eval(str1);
eval(str2);
eval(str3);
eval(str4);
This entry was posted in nodejs. Bookmark the permalink.

Leave a Reply