node.js

node.js

你可以将数据存储在变量中,并在你需要的时候使用它。
变量声明需要使用 var 关键字,如果没有使用 var 关键字变量会直接打印出来。
使用 var 关键字的变量可以使用 console.log() 来输出变量。

> x = 10
10
> var y = 10
undefined
> x + y
20
> console.log("Hello World")
Hello World
undefined
> console.log("www.runoob.com")
www.runoob.com
undefined

下划线()变量
你可以使用下划线(
)获取上一个表达式的运算结果:

1.js中三种定义变量的方式const, var, let的区别。

1.const定义的变量不可以修改,而且必须初始化。
1 const b = 2;//正确
2 // const b;//错误,必须初始化 
3 console.log('函数外const定义b:' + b);//有输出值
4 // b = 5;
5 // console.log('函数外修改const定义b:' + b);//无法输出

2.var定义的变量可以修改,如果不初始化会输出undefined,不会报错。

1 var a = 1;
2 // var a;//不会报错
3 console.log('函数外var定义a:' + a);//可以输出a=1
4 function change(){
5 a = 4;
6 console.log('函数内var定义a:' + a);//可以输出a=4
7 } 
8 change();
9 console.log('函数调用后var定义a为函数内部修改值:' + a);//可以输出a=4

3.let是块级作用域,函数内部使用let定义后,对函数外部无影响。

1 let c = 3;
2 console.log('函数外let定义c:' + c);//输出c=3
3 function change(){
4 let c = 6;
5 console.log('函数内let定义c:' + c);//输出c=6
6 } 
7 change();
8 console.log('函数调用后let定义c不受函数内部定义影响:' + c);//输出c=3

undefined与null

undefined

变量声明且没有赋值
获取对象中不存在的属性是
函数需要实参,但是调用时没有传值,形参是undefined
函数调用没有返回值或者return后没有数据,接受函数返回的变量是undefined

null

对象不存在就是mull

循环语句

if(){
}else if{
}else{
}
for(i;i;i){}
while(i){}
do{}while()

运用循环打印数组

var ar=[1,2];
var arr=new Array();
var ar=['a','b','c'];
for(var i=0;i<ar.length,i++){
console.log(ar[i]);
}
等价于
for (i in ar){
console.log(ar[i]);
}
var ar=['a','b','c'];
ar.forEach(function (a,b,c){
    console.log(a);
    console.log(b);
    console.log(c);
})
打印出
a
0
[ 'a', 'b', 'c' ]
b
1
[ 'a', 'b', 'c' ]
c
2
[ 'a', 'b', 'c' ]

定义对象

var obj1={name:'某人',age:56,fun:function(){
alert(222);
}}
console.log(obj1.name);
console.log(obj1.age);
console.log(obj1.fun);
console.log(obj1.fun());(函数没有返回值)

打印

某人
56
[Function: fun]
/home/qfr/IdeaProjects/newone/src/try.js:2
        alert(222);
        ^

ReferenceError: alert is not defined

函数

创建

字面量(声明前后都可以调用)
function f1(){
}
表达式声明(声明之后才可以调用)
var  f2 =function(){
}
内置构造函数(一般不用)
var f3=new function(){
};

使用

f1();
f2();
f1(f2);(回调函数,函数自身的作用域在声明的地方,而不是调用的地方)

用=>定义函数

var f   = v => v; 
相当于:
var function(v){
    return v;
}
var f = ()=>5;
相当于
var function(){
    return 5;
}
var sum = (a,b)=>a+b;
相当于
var sum=function(a,b){
    return a+b;
}
const full = ({first, last}) => first +"  "+ last;
相当于
const full=function(persion){
    return persion.first+" "+persion.last;
}
[1,2,3].map(function(x){  
    return x*x;  
});  
相当于
[1,2,3].map(x=>x*x)
var result = values.sort(function(a, b){  
    return a -b;  
});
相当于
var result = values.sort((ab)=>a-b)

输入和输出

输出语句

alert()  浏览器弹出警示框
console.log() 浏览器控制台打印输出信息
prompt() 浏览器弹出输入框,用户可以输入
console.log('我的%s已经%d岁', '猫', 2)
%s 会格式化变量为字符串
%d 会格式化变量为数字
%i 会格式化变量为其整数部分
%o 会格式化变量为对象

输入语句

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

readline.question(`你叫什么名字?`, name => {
  console.log(`你好 ${name}!`)
  readline.close()
})

或者


读取文件

var fs =require('fs');
fs.readfile('./tt.txt',[utf8],function(err,data){});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值