不支持Closures闭包
//不支持写法
function computeSum(arr: i32[]): i32 {
var sum = 0
arr.forEach(value => {
sum += value // fails
})
return sum
}
//支持写法1
var sum: i32 // becomes a WebAssembly Global
function computeSum(arr: i32[]): i32 {
sum = 0
arr.forEach(value => {
sum += value // works
})
return sum
}
//支持写法2
function computeSum(arr: i32[]): i32 {
var sum = 0
for (let i = 0, k = arr.length; i < k; ++i) {
sum += arr[i] // works
}
return sum
}
// 不支持写法
const p = new Promise((resolve, reject)=>{
});
// 不支持写法
async foo():void{
}
await foo();
函数声明必须在全局,不能在函数内
// 不支持写法
function play(): void{
function study(): void{
//....
}
}
// 支持写法
function play(): void{
//..
}
function study(): void{
//....
}
[]在AssemblyScript中
// 不支持
const names = ["xiaoming", 1235]; // 数据类型必须一致
// 支持
const names = ["xiaoming", "xiaowang"];
等同于
const names = new Array<string>();
names.push("xiaoming");
names.push("xiaowang");
{}在AssemblyScript中
// 不支持
const stu = {name:'小明'};
// 支持,必须声明类型
class Student{
name: string;
}
const stu:Student = {name:'小明'};
没有Object,必须是严格类型对象
// 不支持
var a = {}
a.prop = "hello world"
// 支持
var a = new Map<string,string>()
a.set("prop", "hello world")
// 支持
class A {
constructor(public prop: string) {}
}
var a = new A("hello world")
// 支持
declare class A{
prop: string;
}
let a: A = {prop: "hello world"};
number操作
- JS中数字类型为number
- as中数字类型是更具体的i32,i64,f64,f32等,as中number默认为f64 ```typescript // float转int const num: number = 1.24; // 取整数(不四舍五入) const int_num: i64 = num as i64; const int_num: i64 = Math.floor(num) as i64;
// 字符串转数字 // 错误写法(坑),parseInt返回值为f64类型, 官网是错的 const int_num: i64 = parseInt(“1.24”); // 正确写法 const int_num: i64 = parseInt(“1.24”) as i64;
<a name="6dWoQ"></a>
#### [不支持any 和 undefined](https://www.assemblyscript.org/stdlib/number.html#integers)
```javascript
// 不支持写法
function foo(a?) {
var b = a + 1
return b
}
// 支持写法
function foo(a: i32 = 0): i32 {
var b = a + 1
return b
}
不支持union types联合类型
// 不支持写法
function foo(a: f64 | string): void {
}
// 支持写法T,在内部判断
function foo<T>(a: T): void {
if(isString<T>(a)){
}else if(isInteger<T>(a)){
}
}
不支持正则Regex
// 不支持写法
var myRe = new RegExp('d(b+)d', 'g');
var myArray = myRe.exec('cdbbdbsbz');
可以通过第三方库支持:https://www.assemblyscript.org/status.html#regexp
不支持JSON
// 不支持写法
const obj = JSON.parse('{"name":"jack"}');
推荐用waft-json:npm i waft-json
也可以通过第三方库支持:https://github.com/nearprotocol/assemblyscript-json
不含定时器API
可以通过waft框架提供定时器的API使用。
// 不支持
setTimeout(()=>{
},500)
// 不支持
setInterval(()=>{
},500)
不支持?参数省略
// 不支持写法
const foo = (a: string, b?: string):void =>{}
foo('name1');
// 支持写法
const foo = (a: string, b: string = "name2"):void =>{}
foo('name1')
不支持异常处理和try catch
// 不支持写法
function doThrow(): void {
throw new Error("message")
}
// 不支持写法
try{
doThrow()
}catch(e){
}
不支持 ===, 支持 ==
// 不支持写法
let path = event.data.getString('path') === 'pages/index/index' ? 'index' : 'wordcase';
// 支持语法
let path = event.data.getString('path') == 'pages/index/index' ? 'index' : 'wordcase';