JS三要素:
ECMAScript JS核心语法,语法标准
DOM(Document Object Modal):文本对象模型
对原生DOM节点进行操作,API
BOM(Browser Object Modal):浏览器对象模型
JS数据类型以及之间的转换
基本数据类型:String Number Boolean undefined null
引用数据类型:Object array function RegExp JSON 自定义引入类型
其他转换为String类型:
- +号拼接’’(最简单方法)
- toString()函数
- String()
其他类型转换成boolean类型
- !!
- Boolean()
转换为boolean为false的值有:0 NAN null undefined
其他类型转换成number类型
- +/- 其他数据类型
- Number()
- parseInt()函数 (如果转换的值是String类型的’010’不会当作八进制转换,结果为10;)
如果转换的值是null,undefined,boolean,均转换为NaN;如果转换的值是number类型的小数,则保留整数
值传递和引用传递
- 值传递针对的基本数据类型,var a=1; b=a; a向b传递的是具体的值
引用传递针对的引用数据类型,是指针地址的传递
基本数据类型的所有内容都是在栈区;引用数据类型变量名、指针地址在栈区、具体内容和指针地址在堆区。
下面列举二者的基本应用:
对象创建的方法
- 字面量创建对象法
eg: var obj={};
var obj1={
id :’1001’,
name : ‘zs’,
age :18,
sayName:function(){
console.log(‘say Name’);
},
products ={
name =’笔记本’,
price=300
},
user:user
}
此创建方法,里面的属性由键值对组成,key、value用:分割,属性与属性之间用,分割,最后一个属性结束不用写,
- 构造函数创建对象方法
eg :
var person=new Object();//相当于var person={};
person.id=’1001’;
person.name=’zs’;
person.sayAge = function(){
console.log(‘say Age’);
}
此创建方法,先写构造函数,再用.访问法添加属性
对象的访问
- 点访问
function add(a, b) {
console.log(a + b);
return a + b;
}
add(5, 5);//10
person.plus2 = add(10, 20); //30
console.log(person.plus2, typeof person.plus2); //30 number
- []访问法
//中括号应用场景:key中有特殊符号
//完全遍历 增强版的for循环:for...in
//语法:for(var 自定义变量名保存属性 in 对象/数组){}
person['first name'] = '张';
var obj4 = {
name: 'zs',
'first name': 'zs',
'age': 12
};
console.log(obj4['first name']);
var n = "first name";
console.log(obj4[n]);
for (var key in person) {
var value = person[key];
console.log(key, value, person[key]);
}
函数
函数的声明
第一种:function 函数名(形参){
函数体或代码块
}
eg :
function add(v1, v2) { //形参
console.log('add');
v1 = Number(v1);
v2 = Number(v2);
var result = v1 + v2; //return 是局部变量
return result; //return 的是result存放的值
}
console.log(add);
console.log(add());
console.log(add(3, 4));
第二种:var 函数名=function (){
函数体或代码块
}
//函数定义第二种方法
var print = function() {
console.log('打印');
return;
}
print();
//形参、实参
function add(v1, v2) {
console.log(v1, v2);
console.log(add.length);
console.log('内部变量', arguments);
console.log('arguments不被枚举到的值', arguments.length, arguments.callee);
}
//形参的个数: 函数名称.length
//实参的个数:arguments.length
//arguments.callee指的是调用具有arguments的函数自己
// arguments.length,arguments.callee只能写在函数体内
console.log(add.length); //length:形参的个数
eg :
start = 0;
var foo = function() {
console.log(arguments.callee); //等价于foo();
// arguments.callee(); 调用自己,现在自己是function,死循环,所以得写一个判断条件
start++;
if (start < 10) {
arguments.callee(); //等价于foo();
}
}
foo();
//函数名.call(执行环境对象,实参列表);
//函数名.apply(执行环境对象,实参列表数组);
//执行环境对象:函数作为对象中的成员
//call与apply的功能与区别
//功能:反建立函数与对象之间的关系
function print(color1, color2) {
console.log(this.price, color1, color2);
}
print('red', 'blue');//undefined red blue
print.call(this, 'red', 'blue', 'green');//undefined red blue
print.apply(this, ['red', 'blue', 'green']);//undefined red blue
var p1 = { name: '连衣裙', price: 100 };
var p2 = { name: '笔记本', price: 2000, print };
print.call(p1, 'red', 'green');//100 red green
print.apply(p1, ['red', 'green']);//100 red green
p2.print('red');//2000 red undefined
print.call(p2, 'red');//2000 red undefined
print.apply(p2, ['red']);//200 red undefined