类型与运算符
类型
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators
特殊的 undefined
, NaN
, Infinity
, null
- 前三者都是全局对象的一个属性。假设一个函数没有使用 return 语句指定返回值,就会返回一个
undefined
值 - null 是一个字面量,表示尚未创建的对象
类型判断
typeof
var word = 'hello';console.log(typeof word);
常用内置对象
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
String
Array
Map
JSON
JSON.parse()
Parse the string text as JSON, optionally transform the produced value and its properties, and return the value
JSON.stringify()
Return a JSON string corresponding to the specified value, optionally including only certain properties or replacing property values in a user-defined manne
Number
i.e. Number(‘123’)
parseInt(),parseFloat()
Math
Date
RegExp
Proxy
Reflect
变量、作用域、判断与循环
https://developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Firststeps/Variables#var%E4%B8%8Elet%E7%9A%84%E5%8C%BA%E5%88%AB (留意变量提升问题) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#description (留意 TDZ 问题) https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration
var
//Just like const the let does not create properties of the window object when declared globally (in the top-most scope)
let x = 1;
if (x === 1) {
let x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 1
//This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do not become properties of the window object, unlike var variables.
const number = 42;
try {
number = 99;
} catch (err) {
console.log(err);
// expected output: TypeError: invalid assignment to const `number'
// Note - error messages will vary depending on browser
}
console.log(number);
// expected output: 42
if (...) {...} else {...}
switch (...) { case (): ... case (): ...}
for (var ... ; ...; ...;) {...}
var step;
for (step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log('Walking east one step');
}
for (variable in object) {
statements
}
// for...in 循环遍历的结果是数组元素的下标;而 for...of 遍历的结果是元素的值
// 在 python 中,for...in... 与 js 的 for...of...相同
let arr = [3, 5, 7];
arr.foo = "hello";
for (let i in arr) {
console.log(i); // 输出 "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // 输出 "3", "5", "7"
}
生成器与迭代器
函数与表达式
function name() {}
, 匿名函数 ()
, 箭头函数 () => {}
var json = JSON.parse(pm.request.body.raw);
setTimeout(() => {
pm.environment.unset("schema_name");
pm.environment.set("schema_name", json.Name);
console.log('schema_name was set successfully')
console.log("schema_name:" + pm.environment.get('schema_name'))
}, 3000)
function random(number) {
return Math.floor(Math.random()*number);
}
function generateRdStr() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
Date.parse(new Date())
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript#numbers
parseInt('12'); // 12
var num = 1; num.toString()
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
异常
try{...} catch (e) {...} finally {...}
并发模型与事件循环
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/EventLoop
async
, await
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
var x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();
promise
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 300);
});
promise1.then((value) => {
console.log(value);
// expected output: "foo"
});
console.log(promise1);
// expected output: [object Promise]
抽象语法树
继承、原型与原型链
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Inheritance_and_the_prototype_chain