本文记录 node.js 最基本的语法。
Node.js有一些核心类型:number,boolean,string,object、undefined 和 function。
可以使用 typeof 查看数据类型
数字
typeof(3.5)
typeof(3)true 和 false
typeof(true)
typeof(false)字符串
typeof('abc')node.js 对象,由 {} 定义 或 new Object();
var user = {
first_name: "HTML",
last_name: "CSS",
age: 32,
};
var empty = new Object()
console.log(typeof(user))
console.log(typeof(empty))意味着尚未设置或根本不存在
var a
typeof(a)
typeof(askfalsjdflasf)函数类型
function hello(name) {
console.log("hello " + name);
}
hello('abc')
console.log(typeof(hello))var a = 10; // 赋值语句
a = 4 * 23 + 188 / 6; // 数学表达式
var c = 7;
var b = (a + c) / 2; // 变量的数学表达式
console.log(a);
console.log(b);// 比较表达式,如果成立,那么为true,否则为false;
console.log(a > b);
console.log(a == b);
console.log(a <= b);
console.log(a != b);//&&所有条件都满足才为真
//||所有条件中只要有一条满足就为真
console.log("&&&&&&&&&");
console.log(false && false);
console.log(false && true);
console.log(true && false);
console.log(true && true);
// 逻辑或
console.log("||||||||||||");
console.log(false || false);
console.log(false || true);
console.log(true || false);
console.log(true || true);
console.log((false && false) || true); // true
console.log(false && (false || true)); // false
// 非
console.log(!true)
console.log(!false)// 字符串的加法是把各个字符串连接在一起,生成一个新的字符串;
var str = "Hello i am ";
var name = "a girl"
var str_total = (str + name);
console.log(str, name, str_total);
// 字符串与基本的数据类型相加,把这个基本数据类型转成字符串后再生成新的字符串
var num_str = " " + a + b + c + true;
console.log(num_str);
// 简约的写法
a += c; // a = a + c;
a -= c; // a = a - c;
a *= c; // a = a * c;
a /= c; // a = a / c;a ++; // a = a + 1;
++ a; // a = a + 1;
a --; // a = a - 1;
-- a; // a = a - 1;语法
if (条件判断)
{
主体
}示例
> if (a > b)
{
console.log("a is bigger")
}语法
if (条件判断)
{
主体
} else
{
主体
}示例
if (a > b)
{
console.log("a is bigger")
}
else
{
console.log("b is bigger")
}语法
if(条件判断)
{
}
else if {
}
...
else {
}示例
if (a == 1)
{
console.log("a is 1")
}
else if(a == 2)
{
console.log("a is 2")
}
else if(a == 3)
{
console.log("a is 3")
}
else if(a == 4)
{
console.log("a is 4")
}
else
{
console.log("a is " + a)
}语法
switch(变量) {
case 常量1:
break;
case 常量2:
break;
}示例
var a = 2
switch (a)
{
case 1:
{
console.log("a is 1")
break
}
case 2:
{
console.log("a is 2")
break
}
case 3:
{
console.log("a is 3")
break
}
case 4:
{
console.log("a is 4")
break
}
default:
{
console.log("final a is " + a)
}
}语法
while(表达式) { 循环体}示例
//while(条件成立) {循环体代码};
index = 0;
while(index < 10) { // 循环执行了10次;判断一下, 循环条件是否为真,为真就执行,否者就不执行循环体;
console.log(index);
index += 2;
}
console.log("out of while");
/*
// 死循环,如果任何时候,这个循环判断条件都为真,那么就是死循环,代码就在循环体里面一直执行;
while(true) { // 死循环,一直执行while语句
console.log("while call true");
}
*/语法
for(初始化; 循环条件; 循环迭代) {循环体}示例
var walk = 0;
var i = 1;
for(walk = 0, i = 1; i <= 10; walk += 2, i ++) {
console.log(walk);
}
walk = 0;
i = 1;
for(; i <= 10; walk += 2, i ++) {
}
console.log("out for");
// 死循环,如果么有循环判断,默认为true
/*for(;;) {
console.log("loop for");
}*/
// 省略初始化与循环迭代语句;
i = 6;
for(; i < 10;) {
console.log("for loop");
i ++;
}语法
do {}while(表达式);示例
// for, while是先判断后执行,do{} while(); 先执行, 再判断是否继续执行循环
i = 0;
do {
i ++;
console.log(i);
}while(i < 10);终止当前循环体的本次循环
i = 1;
for(i = 1; i <= 10; i ++) {
if (i == 8) {
continue; // 终止本次循环,但是仍然会执行 循环迭代语句 (i ++);
}
console.log("give hime " + i);
}
i = 1;
while(i <= 10) {
if (i == 8) {
console.log("while continue");
i ++;
continue; // 终止本次循环,回到循环判断,一定要注意循环判断变量,在continue,不要有死循环
}
console.log("while give hime " + i);
i ++;
}结束最近一个循环体所有的循环
i = 1;
for(i = 1; i <= 10; i ++) {
if (i == 8) {
break; // 终止所有循环,跳出循环代码,但是不会执行 循环迭代语句 (i ++);
}
console.log("give hime " + i);
}
console.log("end_for", i); // i = 8;
i = 1;
while(i <= 10) {
if (i == 8) {
console.log("while continue");
i ++;
break; // 终止所有循环,跳出循环代码
}
console.log("while give hime " + i);
i ++;
}
console.log("end while");要查找具有另一个字符串的字符串,请使用indexOf函数:
var i = "this is a test".indexOf("is");
console.log(i);
->
2要从字符串中提取子字符串,请使用substr或splice函数。
substr 获取要提取的字符串的起始索引和长度。splice取起始索引和结束索引:
var s = "this is a test string."
console.log(s.substr(19, 3))
console.log(s.substr(-5))
console.log(s.slice(19, 22))
console.log(s.slice(-5))
->
ng.
ring.
ng.
ring.要将字符串拆分为子字符串,请使用split函数并获取数组作为结果:
var s = "a|b|c|d|e|f|g|h".split("|");
console.log(s);
->
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']trim函数从字符串的开头和结尾删除空格:
var s = " cat \n\n\n ". trim();
console.log(s);
->
catreplace替换字符串
var s = "cat"
console.log(s.replace('cat', 'dog'))
->
dog属性 | 描述 |
|---|---|
constructor | 对创建该对象的函数的引用 |
length | 字符串的长度 |
prototype | 允许您向对象添加属性和方法 |
方法 | 描述 |
|---|---|
charAt() | 返回在指定位置的字符。 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 |
concat() | 连接两个或更多字符串,并返回新的字符串。 |
fromCharCode() | 将 Unicode 编码转为字符。 |
indexOf() | 返回某个指定的字符串值在字符串中首次出现的位置。 |
lastIndexOf() | 从后向前搜索字符串。 |
match() | 查找找到一个或多个正则表达式的匹配。 |
replace() | 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串。 |
search() | 查找与正则表达式相匹配的值。 |
slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分。 |
split() | 把字符串分割为字符串数组。 |
substr() | 从起始索引号提取字符串中指定数目的字符。 |
substring() | 提取字符串中两个指定的索引号之间的字符。 |
toLowerCase() | 把字符串转换为小写。 |
toUpperCase() | 把字符串转换为大写。 |
trim() | 去除字符串两边的空白 |
valueOf() | 返回某个字符串对象的原始值。 |
HTML 包装方法返回加入了适当HTML标签的字符串。
方法 | 描述 |
|---|---|
anchor() | 创建 HTML 锚。 |
big() | 用大号字体显示字符串。 |
blink() | 显示闪动字符串。 |
bold() | 使用粗体显示字符串。 |
fixed() | 以打字机文本显示字符串。 |
fontcolor() | 使用指定的颜色来显示字符串。 |
fontsize() | 使用指定的尺寸来显示字符串。 |
italics() | 使用斜体显示字符串。 |
link() | 将字符串显示为链接。 |
small() | 使用小字号来显示字符串。 |
strike() | 用于显示加删除线的字符串。 |
sub() | 把字符串显示为下标。 |
sup() | 把字符串显示为上标。 |
完整介绍:https://www.w3cschool.cn/jsref/jsref-obj-math.html
属性 | 描述 |
|---|---|
E | 返回算术常量 e,即自然对数的底数(约等于2.718)。 |
LN2 | 返回 2 的自然对数(约等于0.693)。 |
LN10 | 返回 10 的自然对数(约等于2.302)。 |
LOG2E | 返回以 2 为底的 e 的对数(约等于 1.414)。 |
LOG10E | 返回以 10 为底的 e 的对数(约等于0.434)。 |
PI | 返回圆周率(约等于3.14159)。 |
SQRT1_2 | 返回返回 2 的平方根的倒数(约等于 0.707)。 |
SQRT2 | 返回 2 的平方根(约等于 1.414)。 |
方法 | 描述 |
|---|---|
abs(x) | 返回 x 的绝对值。 |
acos(x) | 返回 x 的反余弦值。 |
asin(x) | 返回 x 的反正弦值。 |
atan(x) | 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。 |
atan2(y,x) | 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。 |
ceil(x) | 对数进行上舍入。 |
cos(x) | 返回数的余弦。 |
exp(x) | 返回 Ex 的指数。 |
floor(x) | 对 x 进行下舍入。 |
log(x) | 返回数的自然对数(底为e)。 |
max(x,y,z,…,n) | 返回 x,y,z,…,n 中的最高值。 |
min(x,y,z,…,n) | 返回 x,y,z,…,n中的最低值。 |
pow(x,y) | 返回 x 的 y 次幂。 |
random() | 返回 0 ~ 1 之间的随机数。 |
round(x) | 把数四舍五入为最接近的整数。 |
sin(x) | 返回数的正弦。 |
sqrt(x) | 返回数的平方根。 |
tan(x) | 返回角的正切。 |