1. 对象
1.1 增删改查
var teacher = {
height: 180;
weight: 130;
teach: function(){
console.log('I am teaching JS');
},
eat: function(){
console.log('I am having a dinner);
}
}
teacher.address = '北京'; //增
teacher.height = 155; //改
delete teacher.height; //删
delete teache.teach; //删
1.2 数组方法
1.arr.indexOf();//找数组下标
2.arr.splice(x,y);//从数组x位删除y个值
3.arr.push(x);//从数组最后一位添加x值
2. 构造函数
2.1 构造函数
/*1.对象字面量、对象直接量*/
var obj = {
name: '张三',
sex: 'male'
}
/*2.系统自带的构造函数 -> 较少使用*/
var obj = new Object();//与对象字面量相等
obj.name = '张三';
obj.sex = '男';
console.log(obj);
/*
3.自定义构造函数 -> 大量使用
大驼峰
*/
function Teacher(){
this.name = '张三';
this.sex = '男士';
this.somke = function(){
console.log('I am smoking');
}
}
var teacher = new Teacher();
3. 作业
/*
1.写一个构造函数,接受数字类型的参数,参数数量不定,完成参数相加和相乘的功能
*/
function Compute() {
var res = 0;
this.add = function () {
loop(arguments, "add", res);
};
this.mul = function () {
res = 1;
loop(arguments, "mul", res);
};
function loop(args, method, res) {
for (var i = 0; i < args.length; i++) {
var item = args[i];
if (method === "add") {
res += item;
} else if (method === "mul") {
res *= item;
}
}
console.log(res);
}
}
var compute = new Compute();
compute.add(2, 4, 6);
compute.mul(2, 4, 6);
/*
2.写一个构造车的函数,可设置车的品牌、颜色、排量
再写一个构造消费者的函数,设置用户的名字、年龄、收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性
*/