1.对象
1.1 字面量创建对象
var teacher = {name: '张三',age: 32,sex: 'male',height: 176,weight: 130,teach: function(){console.log('I am teacching JavaScript');},smoke: function(){console.log('I am smoking');},eating: function(){console.log('I am having a dinner');}}// 查console.log(teacher.name);console.log(teacher.weight);console.log(teacher['height']);teacher.teach();// 增加属性和方法teacher.address = '北京';teacher.eat = function(){console.log('I am having a dinner');}consolr.log(teacher);// 修改属性和方法teacher.height = 180;teacher.teach = function(){console.log('I am teaching HTML');}console.log(teacher);console.log(teacher.teach);// 删除属性和方法delete teacher.address;// delete teacher.teach(); // 会打印出来,是执行 因为teach() 调用了delete teacher.taech;console.log(teacher);
1.2 调用方法从而改变属性值
var teacher = {name: '张三',age: 32,sex: 'male',height: 176,weight: 130,teach: function(){console.log('I am teaching JavaScript');},smoke: function(){this. weight--;console.log(this.weight);},eat: function(){this.weight++;console.log(this.weight);}}taecher.smoke(); // 129 => taecher.weight: 129teacher.smoke(); // 128teacher.eat(); // 129
var attendance = {students: [],total: 6,join: function(name){this.students.push(name);if(this.students.length === this.total){console.log(name + '到课, 学生已到齐');}else{console.log(name + '到课, 学生未到齐');}},leave: function(name){var idx = this.students.indexOf(name);if(idx !== -1){this.students.splice(idx, 1);}console.log(name + '早退');console.log(this.students);},classOver: function(){console.log('下课了');this.students = [];}}attendance.join('小红');attendance.join('小王');attendance.join('小张');attendance.join('小李');attendance.join('小刘');attendance.join('小吴');attendance.leave('小李');attendance.classOver();
2. 构造函数创建对象
2.1 系统构造函数创建对象
与对象字面量相等
对象和构造函数是俩码事,对象是通过实例化构造函数而得出的对象实例
// 对象字面量,对象直接量var obj = {name: '张三',sex: 'male'}// 构造函数,系统自带的Object函数var obj = new Object();obj.name = '张三';obj.sex = 'male';
2.2 自定义构造函数
// // 大驼峰function Teacher(name, sex, weight, course){this.name = name;this.sex = sex;this.weight = weight;this.course = course;this.somke = function(){this.weight--;console.log(this.weight);};this.eat = function(){this.weight++;console.log(this.weight);}}var teacher1 = new Teacher('xiaohong', 'male', 140, 'Java');var teacher2 = new Teacher('xiaoming', 'male', 150, 'JavaScript');console.log(teacher1, teacher2);
function Teacher(opt){this.name = opt.name;this.sex = opt.sex;this.weight = opt.weight;this.course = opt.course;this.somke = function(){this.weight--;console.log(this.weight);};this.eat = function(){this.weight++;console.log(this.weight);}}var teacher1 = new Teacher({name:'小红',sex: 'women',weight: 100,course: 'JavaScript'});var teacher2 = new Teacher({name:'小宁',sex: 'male',weight: 160,course: 'Java'});console.log(teacher1, teacher2);
3. 练习
1. 写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能
function AddMul(){var args = arguments,res;this.add = function(){res = 0;loop('add', res);}this.mul = function(){res = 1;loop('mul', res);}function loop(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 am1 = new AddMul(1, 2, 3, 4);am1.add();am1.mul();
2. 写一个构造车的函数,可以设置车的品牌,颜色,排量写一个构造消费者的函数,可以设用户的名字,年龄,收入 通过选车的方法,实例化该用户喜欢的车,在设置车的属性
function Car(opt){this.brand = opt.brand,this.color = opt.color,this.displacement = opt.displacementthis.myCar = function(){console.log(`My car brand is ${this.brand}, coloris ${this.color}, displacement is ${this.displacement}`);console.log(this.displacement);}}function Consumer(user){this.name = user.name,this.age = user.age,this.earning = user.earning,this.choiceCar = function(){return new Car(user.opt);}}var c1 = new Consumer({name: 'xiaomeng',age: 16,earning: '30K',opt:{brand: 'benz',color: 'white',displacement: 3.0}});var car = c1.choiceCar();console.log(car);car.myCar();
