支持JavaScript基础语法和数据类型,以及数组、集合、字典、对象操作。 支持require引用模块。 二、安装 1 install-package AScript 2 install-pac
支持JavaScript基础语法和数据类型以及数组、集合、字典、对象操作。支持require引用模块。二、安装1 install-package AScript2 install-package AScript.Lang.JavaScript三、使用说明1、注册语言1 Script.Langs.Set(“js”, JavaScriptLang.Instance);2 // 可全局设置为默认语言3 // Script.Langs.Set(“js”, JavaScriptLang.Instance, setDefault: true);2、定义函数复制代码1 var s 2 function sum(a,b) {3 return ab;4 }5 sum(10,20)6 ;7 var script new Script();8 script.Context.Langs new [] { “js” };9 var result script.Eval(s);10 Assert.AreEqual(30L, result);复制代码3、正则表达式js中使用 /{pattern}/{options} 格式表示正则表达式。复制代码1 var script new Script();2 script.Context.Langs new[] { “js” };3 var result script.EvalList(“‘hello world World’.match(/world/gi)”);4 Assert.AreEqual(2, result.Count);5 Assert.AreEqual(“world”, result[0]);6 Assert.AreEqual(“World”, result[1]);复制代码4、解构1对象解构支持默认值、别名、嵌套解构。复制代码1 var s 2 var {name, age} {name: ‘Alice’, age: 25}; // name‘Alice’, age253 var {a, b 100} {a: 10}; // a10, b1004 var {inner: {name}} {inner: {name: ‘Tom’, age: 20}}; // name‘Tom’5 var {name: aliasName} {name: ‘Bob’}; // aliasName‘Bob’6 ;7 var script new Script();8 script.Context.Langs new[] { “js” };9 script.Eval(s);复制代码2数组解构支持默认值、嵌套解构复制代码1 var s 2 var [x, y, z] [10, 20, 30]; // x10, y20, z303 var [x, , z] [1, 2, 3]; // x1, z34 var [a 5, b 10] [3]; // a3, b105 var [[inner], outer] [[10], 20]; // inner10, outer206 ;7 var script new Script();8 script.Context.Langs new[] { “js” };9 script.Eval(s);复制代码3混合解构对象、数组混合解构1 var script new Script();2 script.Context.Langs new[] { “js” };3 script.Eval(“var [first, {name}] [1, {name: ‘John’}]”);4 Assert.AreEqual(1L, script.Eval(“first”));5 Assert.AreEqual(“John”, script.Eval(“name”));5、字符串插值复制代码1 var s 2 var name‘tom’;3hello ${name}, 58${58}4 ;5 var script new Script();6 script.Context.Langs new [] { “js” };7 Assert.AreEqual(“hello tom, 5813”, script.Eval(s));复制代码6、字符串函数复制代码1 String.fromCharCode(65,66,67); // ‘ABC’2 ‘hello’.startsWith(‘he’); // true3 ‘hello’.endsWith(‘a’); // false4 ‘hello’.includes(‘e’); // true5 ‘hello’.indexOf(‘el’); // 16 ‘hello’.indexOf(‘el’, 2); // -17 ‘hello’.lastIndexOf(‘l’); // 38 ‘hello’.search(‘el’); // 19 ‘hello’.search(/el/gi); // 110 ‘hello’.substr(-2); // ‘lo’11 ‘hello’.substr(-2,1); // ‘l’12 ‘hello’.substring(2); // ‘llo’13 ‘hello’.substring(2,4); // ‘ll’14 ‘hello’.slice(2); // ‘llo’15 ‘hello’.slice(2,4); // ‘ll’16 ‘hello’.slice(-3); // ‘llo’17 ‘hello’.slice(-3,-1); // ‘ll’18 ‘Hello’.toLowerCase(); // ‘hello’19 ‘Hello’.toUpperCase(); // ‘HELLO’20 ’ hello ‘.trim(); // ‘hello’21 ’ hello ‘.trimStart(); // ‘hello ’22 ’ hello ‘.trimEnd(); // ’ hello’23 ‘hello’.padStart(7); // ’ hello’24 ‘hello’.padStart(7, ‘x’); // ‘xxhello’25 ‘hello’.padEnd(7); // ‘hello ’26 ‘hello’.padEnd(7, ‘x’); // ‘helloxx’27 ‘hello’.charAt(1); // ‘e’28 ‘hello’.charCodeAt(1); // 101L29 ‘hello tom, I am Tony’.match(‘to’); // [‘to’]30 ‘hello tom, I am Tony’.match(/to/); // [‘to’]31 ‘hello tom, I am Tony’.match(/to/gi); // [‘to’, ‘To’]32 ‘hello tom, I am Tony’.replace(‘to’, ‘x’); // 替换第1项‘hello xm, I am Tony’33 ‘hello tom, I am Tony’.replaceAll(‘to’, ‘x’); // 替换所有项区分大小写‘hello xm, I am Tony’34 ‘hello tom, I am Tony’.replace(/to/g, ‘x’); // 替换匹配项‘hello xm, I am Tony’35 ‘hello tom, I am Tony’.replace(/to/gi, ‘x’); // 替换匹配项‘hello xm, I am xny’36 ‘a,b,c’.split(’,’); // [‘a’,‘b’,‘c’]37 ‘hello’.repeat(2); // ‘hellohello’38 ‘hello’.concat(’ , ‘world’); // ‘hello world’复制代码7、创建数组对应C#中的List类型。复制代码1 var s 2 var arr1 [1,2,3];3 var arr2 new Array(1,2,3);4 var arr3 new Array(2);5 ;6 var script new Script();7 script.Context.Langs new[] { “js” };8 script.Eval(s);9 var arr1 script.EvalList(“arr1”);10 Assert.AreEqual(3, arr1.Count);11 Assert.AreEqual(1L, arr1[0]);12 Assert.AreEqual(2L, arr1[1]);13 Assert.AreEqual(3L, arr1[2]);14 var arr3 script.EvalList(“arr3”);15 Assert.AreEqual(2, arr3.Count);16 Assert.IsNull(arr3[0]);17 Assert.IsNull(arr3[1]);复制代码8、数组函数复制代码1 [1, 2].concat([3, 4]); // [1, 2, 3, 4]2 [1, 2, 3].join(‘,’); // ‘1,2,3’3 [1, 2, 3].indexOf(2); // 14 [1, 2, 3].includes(2); // true5 [1, 2, 3].reverse(); // [3, 2, 1]6 [1, 2, 3, 4, 5].filter(x x % 2 0); // [2, 4]7 [1, 2, 3].map(x x * 2); // [2, 4, 6]8 [1, 2, 3].reduce((acc, x) acc x); // 69 [1, 2, 3].reduce((acc, x) acc x, 10); // 1610 [2, 4, 6].every(x x % 2 0); // true11 [1, 3, 5].some(x x % 2 0); // false12 [1, 2, 3].find(x x 1); // 213 [1, 2, 3].findIndex(x x 1); // 114 [1, 2, 3].fill(0); // [0, 0, 0]15 [1, 2, 3].forEach(x { }); // 遍历数组16 [1, 2, 3].pop(); // 317 [1, 2, 3].push(5, 6, 7); // 数组结尾处理添加元素18 [1, 2, 3].shift(); // 移除并返回首部元素19 [1, 2, 3].unshift(5, 9, 10); // 从首部插入3个元素20 [1, 2, 3, 4, 5].slice(2, 4); // 截取列表[2,4)[3, 4]21 [1, 2, 3, 4, 5].splice(1, 2); // 截取并返回列表[2, 3]复制代码9、时间Date对应C#中的DateTime类型。复制代码1 new Date(); // 当前时间2 Date.now(); // 当前时间戳3 new Date(2026, 6, 4); // 2026-07-044 var time new Date(‘2026-7-4’);5 time.getYear(); // 获取年份-19001266 time.getMonth(); // 获取月份-167 time.getDate(); // 获取月份中的天(1~31)48 time.getDay(); // 获取星期69 time.getHours();10 time.getMinutes();11 time.getSeconds();12 time.getMilliseconds();13 time.getTime(); // 获取时间戳14 time.toString(‘yyyy-MM-dd’); // ‘2026-07-04’15 time.setFullYear(2025);16 time.setFullYear(2025, 6);17 time.setFullYear(2025, 6, 4);18 time.setHours(21);19 time.setMinutes(36);20 time.setSeconds(15);21 time.setMilliseconds(238);复制代码10、字典Map对应C#中的Dictionaryobject, object类型。复制代码1 var m new Map([[‘a’, 1], [‘b’, 2], [‘c’, 3]]);2 m.set(‘d’, 4);3 m.size; // 44 m.get(‘a’); // 15 m.has(‘a’); // true6 m.delete(‘c’);7 m.clear();8 m.keys();9 m.values();10 m.entries();11 m.forEach(function(value, key) { });复制代码11、集合Set对应C#中的HashSet类型。复制代码1 var set new Set([1, 2, 3]);2 set.add(4);3 set.has(2);4 set.delete(3);5 set.clear();6 set.size;7 set.forEach(x { });复制代码12、Promisejs异步编程对应C#中的Task。then/catch/finally1 var script new Script();2 script.Context.Langs new[] { “js” };3 var result script.Eval(“await new Promise((resolve, reject) resolve(1)).then(x x 1).catch(err{})”);4 Assert.AreEqual(2L, result);Promise.all复制代码1 var s 2 var arr await Promise.all([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]);3 arr[0] arr[1] arr[2]4 “;5 var script new Script();6 script.Context.Langs new[] { “js” };7 var result script.Eval(s);8 Assert.AreEqual(6L, result);复制代码Promise.any1 var script new Script();2 script.Context.Langs new[] { “js” };3 var result script.Eval(“await Promise.any([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)])”);4 Assert.AreEqual(1L, result);13、数学函数复制代码1 Math.abs(-5); // 52 Math.acos(0.5);3 Math.acosh(2);4 Math.asin(0.5);5 Math.asinh(2);6 Math.atan(1);7 Math.atan2(1, 1);8 Math.atanh(0.5);9 Math.tan(0);10 Math.tanh(0);11 Math.sign(5);12 Math.sin(0);13 Math.sinh(0);14 Math.cbrt(27);15 Math.ceil(5.1);16 Math.clz32(1);17 Math.cos(0);18 Math.cosh(0);19 Math.exp(1);20 Math.expm1(1);21 Math.floor(5.9); // 5.022 Math.fround(5.5);23 Math.hypot(3, 4);24 Math.imul(2, 3);25 Math.log(1);26 Math.log10(100);27 Math.log1p(1);28 Math.log2(8);29 Math.max(1, 10, 5); // 10.030 Math.min(1, 10, 5); // 1.031 Math.pow(2, 3); // 8.032 Math.random(); // [0,1)33 Math.round(5.4); // 5.034 Math.sqrt(4); // 2.035 Math.pow(3, 2); // 9.036 Math.trunc(5.9); // 5.037 Math.E;38 Math.PI;39 Math.SQRT2;40 Math.SQRT1_2;41 Math.LN2;42 Math.LN10;43 Math.LOG2E;44 Math.LOG10E;复制代码14、setTimeout/clearTimeout复制代码1 string code ”2 var result 0;3 function onTimeout(a, b) {4 result a b;5 }6 var handle setTimeout(onTimeout, 50, 10, 20);7 handle;8 “;9 var script new Script();10 script.Context.Langs new[] { “js” };11 script.Eval(code);12 Thread.Sleep(100);13 Assert.AreEqual(30L, script.Eval(“result”));复制代码15、setInterval/clearInterval复制代码1 string code ”2 var count 0;3 function handler() { count 1; }4 var timer setInterval(handler, 40);5 timer;6 ;7 var script new Script();8 script.Context.Langs new[] { “js” };9 var timerObj script.Eval(code);10 Thread.Sleep(70);11 script.Eval(“clearInterval(timer)”);12 Assert.AreEqual(1L, script.Eval(“count”));复制代码16、if条件支持对象检测null/undefined/0/false/空字符串为false其他值为true。复制代码1 string code 2 var n 15;3 var result ‘’;4 if (n 10) {5 result ‘less than 10’;6 } else if (n 20) {7 result ‘less than 20’;8 } else {9 result ‘20 or more’;10 }11 result;12 ;13 var script new Script();14 script.Context.Langs new[] { “js” };15 Assert.AreEqual(“less than 20”, script.Eval(code));