目录
4)参数为值传递,传递副本;参数是引用传递,则传递的是地址,操作的是同一个对象。
4.return(如果方法没有返回值,使用return,返回的是undefined)
5.函数的作用域(全局(global variable)与局部(local variable))
一.运算符
二.控制语句
注意点:
三.数组
1.数组定义
1)隐式定义 | var 数组名 = [ ]; (空数组) var 数组名 = [值1,值2,值3,...]; (可以写任意类型) |
2)直接实例化 | var 数组名 = new Array(值1,值2,值3,...); |
3)定义数组并设置长度 | var 数组名 = new Array(size); |
2.数组的基本操作
数组的下标从0开始,不会出现下标越界。
获取数组指定下标的值 | 数组名[下标];(如果下标不存在,则undefined) |
设置数组指定下标的值 | 数组名[下标] = 值;(如果下标不存在,则自动转换) |
获取数组的长度 | 数组名.length; |
设置数组的长度 | 数组名.length = 值; |
设置数组的属性 | 数组名.属性名 = 值; (了解) 数组名["属性名"] = 值; (都不占数组长度) |
获取数组的属性 | 数组名["name"] 或 数组名.name |
了解:如果设置非整数型的下标,则会成为数组的属性,不计入数组的长度。
3.数组的遍历
1)for循环遍历
for (var index = 0;index < 数组长度;index++) {
}
例:for(var i = 0; i < arr3.length ; i++) {
console.log("下标:" + i + ",值:" + arr3[i]);
}
2)for......in
for (var 下标名 in 数组){
}
例:for (var i = 0 in arr3) {
console.log("下标:" + i + ",值:" + arr3[i]);
}
3)forEach循环
数组.forEach(function(element,index){
// element : 元素
// index : 下标
});
例:arr3.forEach(function(element, index){
console.log("下标:" + i + ",值:" + element);
})
for | 不遍历属性 |
for......in | 不遍历索引中的undefined |
forEach | 不遍历属性和索引中的undefined |
(了解)使用数组元素
下标:非负整数(包括整数字符串): | 数组.下标 数组[下标] |
下标: 负数、小数、非数字字符串 : | 数组[属性] |
4.数组提供的操作方法
push | 添加元素到最后 |
indexof | 数组元素索引 |
join | 数组转成字符串 |
补:split (不是数组方法) 字符串方法:将字符串转换成数组
var arr5 = ["a","b","c"];
//添加元素到最后
arr5[arr5.length] = "d";(方法一)
arr5.push("e");(方法二)
console.log(arr5);
//数组元素索引
console.log(arr5.indexOf("a"));
console.log(arr5.indexOf("t")); //找不到返回-1;
//数组转化为字符串
console.log(arr5.join("-"));(中间用“ - ”连接)
//字符串方法:将字符串转换为数组
var str = "1,2,3,4,5";
console.log(str.split(",")); (将用“ , ”连接的字符串,转化为数组)
四.函数(也是对象)
1.函数的定义
1)函数声明语句
function 函数名([参数列表]){
}
例: function foo() {
console.log(1);
}
foo();
2)函数定义表达式
var 变量名/函数名 = function([参数列表]) {
}
3)Function 构造函数(了解)
var 函数名 = new Function([参数列表],函数体);
例://1.函数声明语句
function fn01(a,b) {
console.log(a+b);
}
//2.函数定义表达式
var fn02 = function(a,b) {
console.log(a,b);
}
//3. Function构造函数(了解)
var fn03 = new Function(a,b,'return (a+b);');
//4.即3相当于4
function fn04(a,b) {
return (a + b );
}
2.函数的参数
函数运行时,有时需要提供外部数据,不同的外部数据会得到不同的结果,这种外部数据就叫参数,定义时的参数称为形参,调用时的参数称为实参。
1)实参可以省略,则形参为undefined。
2)如果形参名相同,则以最后一个参数为准。
3)可以设置参数的默认值。
4)参数为值传递,传递副本;参数是引用传递,则传递的是地址,操作的是同一个对象。
//未设置实参
function test01(x,y) {
console.log(x+y);
}
test01();
test01(1);
test01(1,2);
//形参名相同
function test02(x,x) {
console.log(x);
}
test02(1,2);
//可以设置参数的默认值
function test03(x) {
//如果形参x有值,则值为x传递的值,否则值为"x";
x = x || "x";
console.log(x);
}
test03(10); // 10
test03(); // 0
function test04(x) {
(x != null && x != undefined) ? x = x : x = "x";
console.log(x);
}
test04(); // x
test04("Hello"); // Hello
// 参数为值传递,传递副本;参数是引用传递,则传递的是地址,操作的是同一个对象。
//变量(值传递)
var num = 10;
//函数
function test05(num) {
num = 20;
}
//调用函数
test05(num); //实参是定义的变量
console.log(num);
//引用传递
var obj = {name:"zhangsan"};
console.log(obj);
function test06(o) {
o.name = "list";
}
test06(obj);
console.log(obj);
3.函数的调用
1)常用调用方式
函数名([参数列表]);
function f1() {
console.log("常用调用模式.....");
}
f1();
2)函数调用方式(函数有返回值)
var 变量名 = 函数名([参数列表]);
var f2 = function(a) {
console.log("函数调用模式.....");
return a;
}
var num1 = f2(1);
console.log(num1);
3)方法调用模式
对象.函数名([参数列表]);
//定义对象 key键:字符串 value: 任意数据类型
var obje = {
name : "zhangsan", //字符串
age : 18 , //数值
sex : true , //布尔
cats: ["大猫","二猫"], //数组
dog: { //对象
name : "张二狗",
age : 1
},
sayHello:function(){ //函数
console.log("你好呀~");
}
}
console.log(obje);
console.log(obje.dog);
console.log(obje.sayHello);
obje.sayHello(); //对象调用函数
4.return(如果方法没有返回值,使用return,返回的是undefined)
1)在没有返回值的方法中,用来结束方法。
2)在返回值的方法中,一个是用来结束方法,一个是将值带给调用者。
//没有返回值;
function a1() {
console.log("没有返回值.....");
return;
console.log("......."); //不会执行;
}
console.log(a1());
//有返回值;
function a2() {
console.log("有返回值......");
return "test";
console.log("......."); //不会执行;
}
console.log(a2());
5.函数的作用域(全局(global variable)与局部(local variable))
1)全局变量与局部变量同名问题。
2)在函数中定义变量时,若没有加val关键字,使用之后自动变为全局变量。
var pp = "Hello"; //全局变量;
function qq() {
//局部变量
var box = 10;
//全局变量,没有使用var声明,属于全局变量
box2 = 20;
}
qq();
console.log(pp); //全局变量
console.log(box2); //20