标准对象
typeof 123
'number'
typeof true
'boolean'
typeof [12,1]
'object'
typeof 'a'
'string'
typeof {}
'object'
typeof undefined
'undefined'
typeof alert
'function'
typeof NaN
'number'
Date
var now = new Date(); //当前时间
now.getFullYear(); //年
now.getMonth(); //月
now.getDate(); //日
now.getDay(); //星期几
now.getHours(); //时
now.getMinutes(); //分
now.getSeconds(); //秒
now.getTime(); //时间戳 1970-1-1 00:00:00至今毫秒数
console.log(new Date(14141411515)); //时间戳转为时间
//转换
now = new Date(14141411515); //时间戳转为时间
now.toLocaleTimeString()
now.toLocaleString()
now.toUTCString()
JSON对象
JSON是一种轻量级的数据交换格式,层次结构简洁清晰,网络传输效率高
对象{ }
数组[ ]
键值对 key : value
JSON字符串和JS对象的转化
var user = {
name: "yl",
age: 11,
sex: '男'
}
//对象转化为json字符串
var jsonUser = JSON.stringify(user)
//json字符串转化为对象
var obj = JSON.parse(jsonUser) //解析
JSON和JS对象的区别:
{name: 'yl', age: 11, sex: '男'} //JS对象
'{"name":"yl","age":11,"sex":"男"}' //JSON字符串
面向对象
me这个类无法调用person类中的方法,只需定义me这个类的原型为person即可调用
var person = {
name: "杨",
age: 3,
run: function (){
console.log(this.name + "run...");
}
};
var me = {
name: "我"
};
//我的原型是人类
me.proto = person;
class继承
1.class关键字在ES6引入
class Student{
//构造器
constructor(name){
this.name = name;
}
hello(){
alert('hello')
}
}
var xiaoming = new Student("xiaoming");
var xiaolan = new Student("xiaolan");
2.继承
class Person extends Student{
constructor(name, grade){
super(name);
this.grade = grade;
}
myGrade(){
alert("I am a student");
}
}
var xiaoli = new Person("xiaoli");
本质:查看对象原型
原型链:始终指向Object