1.定义变量
var a = 45;//;可要可不要
var b="name";//js是一门弱语言,对象的类型是值得类型
2.定义对象
//在实际应用中描述js对象中,属性可加可不加引号,推荐不加
var people ={"age":18,"name":"张三"};
var people2 ={age:18,name:"张三"};//js的对象可以不严格按照json格式
console.log(people.age);
console.log(people.name)
console.log(people2.age);
console.log(people2.name)
3.定义数组
var arr=[45,23,12];
console.log(arr.length);//打印数组长度,类型可以不一致,但是不推荐,在开发过程中严禁
arr[4] =45;
console.log(arr[4]);//js数组的长度不固定,java数组长度一但确定就不能改变了
4.循环
for (var i =0; i < arr.length; i++){
console.log(arr[i]);
}
5.js数组函数
5.1.push
arr.push(88);//往数组末尾添加数据
5.2.pop
arr.pop();//移除数组的最后一个数据
console.log(arr.length);
for (var i =0; i < arr.length; i++){
console.log(arr[i]);
}
5.3.splice 重要
arr=arr.splice(1,2);//从索引位置开始,删除几个
5.4.typeof
var c=45;
console.log(typeof c);//返回数据类型
5.4.undeFined
var d;
console.log(d);//变量,声明没赋值
5.5.ECMScript标准要求在实现script的时候,undefined == null 必须返回true
var d;
console.log(d);
var e =null;
console.log(e);
console.log(d ==e)
6.流程控制语句
7.1.js在调用方法的时候,可以传入0-N个值,但我们一定要记住方法接受几个参数,那么就传入几个参数,就算参数没有值,传入null
function eat(name, age, eat) {
var str =name +"今年" + age +"岁,"+"正在吃" +eat;
console.log(str);
}
eat('张三',10,'奥利奥','abc');
7.2.js方法调用是按照顺序来传参数
function output(value,str) {
console.log(str)
}
output(null,'Hello Word')
7.3.arguments 函数不定义形参,如何接受数据
function fun() {
//每个方法内置了一个数组参数arguments,可以通过arguments来获取参数的值
console.log(arguments)
console.log('===========================')
console.log(arguments[0])
console.log(arguments[1])
console.log(arguments[3])
console.log(arguments[4])
}
fun(10,20,30,40)
7.4.通过方法类生成对象
//定义一个方法,该方法作用是作为生成对象的一个模板方法
//在具体发定义的时候,如果单纯只是作为一个方法使用,首字母小写
//如果想根据方法来生成1对象,首字母大写
function Person(age,name,eat) {
this.age = age;
this.name = name;
this.eat = eat;
}
var person =new Person(10,'李四')
7.5.通过箭头函数来表示,而箭头函数和java中的lambda表达式异曲同工
//lambda使用前提:只能有且只能有一个抽象方法
ublic class LambdaTest{
public int output(MyInterface myInterface,int a,int b){
return myInterface.add(a,b);
}
public static void main(String[] args) {
LambdaTest test =new LambdaTest();
/* MyInterface myInterface = new Myclass();
int value = test.output(new MyInterface() {
@Override
public int add(int a, int b) {
return a + b;
}
}, 3, 4);*/
int value = test.output((a,b) ->{
return a + b;
},3,4);
System.out.println(value);
}
}
interface MyInterface{
int add(int a,int b);
}
8.闭包 方法中嵌套了方法,方法中的参数的引用会逐渐网上查找