// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This test case is weird in the sense the actual work happens at the eval // at the end. If template strings are not enabled, the test cases would throw // a syntax error and we'd have failure reported. To avoid that, the entire // test case is commented out and is part of a function. We use toString to // get the string version, obtain the actual lines to run, and then use eval to // do the actual evaluation.
// Extra whitespace inside a template substitution is ignored.
assertEq(`${
0
}`, "0");
assertEq(`${ // Comments work in template substitutions. // Even comments that look like code: // 0}`, "FAIL"); /* NOTE: This whole line is a comment.
0}`, "0");
// Template substitutions are expressions, not statements.
syntaxError("`${0;}`");
assertEq(`${{}}`, "[object Object]");
assertEq(`${ function f() { return"ok";
}()
}`, "ok");
// Template substitutions can have side effects. var x = 0;
assertEq(`= ${x += 1}`, "= 1");
assertEq(x, 1);
// The production for a template substitution is Expression, not // AssignmentExpression.
x = 0;
assertEq(`${++x, "o"}k`, "ok");
assertEq(x, 1);
// --> is not a comment inside a template.
assertEq(`
--> this is text
`, "\n--> this is text\n");
// reentrancy function f(n) { if (n === 0) return""; return `${n}${f(n - 1)}`;
}
assertEq(f(9), "987654321");
// Template string substitutions in generator functions can yield. function* g() { return `${yield 1} ${yield 2}`;
}
var it = g(); var next = it.next();
assertEq(next.done, false);
assertEq(next.value, 1);
next = it.next("hello");
assertEq(next.done, false);
assertEq(next.value, 2);
next = it.next("world");
assertEq(next.done, true);
assertEq(next.value, "hello world");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.