1.对象

1.1 字面量创建对象

  1. var teacher = {
  2. name: '张三',
  3. age: 32,
  4. sex: 'male',
  5. height: 176,
  6. weight: 130,
  7. teach: function(){
  8. console.log('I am teacching JavaScript');
  9. },
  10. smoke: function(){
  11. console.log('I am smoking');
  12. },
  13. eating: function(){
  14. console.log('I am having a dinner');
  15. }
  16. }
  17. // 查
  18. console.log(teacher.name);
  19. console.log(teacher.weight);
  20. console.log(teacher['height']);
  21. teacher.teach();
  22. // 增加属性和方法
  23. teacher.address = '北京';
  24. teacher.eat = function(){
  25. console.log('I am having a dinner');
  26. }
  27. consolr.log(teacher);
  28. // 修改属性和方法
  29. teacher.height = 180;
  30. teacher.teach = function(){
  31. console.log('I am teaching HTML');
  32. }
  33. console.log(teacher);
  34. console.log(teacher.teach);
  35. // 删除属性和方法
  36. delete teacher.address;
  37. // delete teacher.teach(); // 会打印出来,是执行 因为teach() 调用了
  38. delete teacher.taech;
  39. console.log(teacher);

1.2 调用方法从而改变属性值

  1. var teacher = {
  2. name: '张三',
  3. age: 32,
  4. sex: 'male',
  5. height: 176,
  6. weight: 130,
  7. teach: function(){
  8. console.log('I am teaching JavaScript');
  9. },
  10. smoke: function(){
  11. this. weight--;
  12. console.log(this.weight);
  13. },
  14. eat: function(){
  15. this.weight++;
  16. console.log(this.weight);
  17. }
  18. }
  19. taecher.smoke(); // 129 => taecher.weight: 129
  20. teacher.smoke(); // 128
  21. teacher.eat(); // 129
  1. var attendance = {
  2. students: [],
  3. total: 6,
  4. join: function(name){
  5. this.students.push(name);
  6. if(this.students.length === this.total){
  7. console.log(name + '到课, 学生已到齐');
  8. }else{
  9. console.log(name + '到课, 学生未到齐');
  10. }
  11. },
  12. leave: function(name){
  13. var idx = this.students.indexOf(name);
  14. if(idx !== -1){
  15. this.students.splice(idx, 1);
  16. }
  17. console.log(name + '早退');
  18. console.log(this.students);
  19. },
  20. classOver: function(){
  21. console.log('下课了');
  22. this.students = [];
  23. }
  24. }
  25. attendance.join('小红');
  26. attendance.join('小王');
  27. attendance.join('小张');
  28. attendance.join('小李');
  29. attendance.join('小刘');
  30. attendance.join('小吴');
  31. attendance.leave('小李');
  32. attendance.classOver();

2. 构造函数创建对象

2.1 系统构造函数创建对象

与对象字面量相等
对象和构造函数是俩码事,对象是通过实例化构造函数而得出的对象实例

  1. // 对象字面量,对象直接量
  2. var obj = {
  3. name: '张三',
  4. sex: 'male'
  5. }
  6. // 构造函数,系统自带的Object函数
  7. var obj = new Object();
  8. obj.name = '张三';
  9. obj.sex = 'male';

2.2 自定义构造函数

  1. // // 大驼峰
  2. function Teacher(name, sex, weight, course){
  3. this.name = name;
  4. this.sex = sex;
  5. this.weight = weight;
  6. this.course = course;
  7. this.somke = function(){
  8. this.weight--;
  9. console.log(this.weight);
  10. };
  11. this.eat = function(){
  12. this.weight++;
  13. console.log(this.weight);
  14. }
  15. }
  16. var teacher1 = new Teacher('xiaohong', 'male', 140, 'Java');
  17. var teacher2 = new Teacher('xiaoming', 'male', 150, 'JavaScript');
  18. console.log(teacher1, teacher2);
  1. function Teacher(opt){
  2. this.name = opt.name;
  3. this.sex = opt.sex;
  4. this.weight = opt.weight;
  5. this.course = opt.course;
  6. this.somke = function(){
  7. this.weight--;
  8. console.log(this.weight);
  9. };
  10. this.eat = function(){
  11. this.weight++;
  12. console.log(this.weight);
  13. }
  14. }
  15. var teacher1 = new Teacher({
  16. name:'小红',
  17. sex: 'women',
  18. weight: 100,
  19. course: 'JavaScript'
  20. });
  21. var teacher2 = new Teacher({
  22. name:'小宁',
  23. sex: 'male',
  24. weight: 160,
  25. course: 'Java'
  26. });
  27. console.log(teacher1, teacher2);

3. 练习

1. 写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能

  1. function AddMul(){
  2. var args = arguments,
  3. res;
  4. this.add = function(){
  5. res = 0;
  6. loop('add', res);
  7. }
  8. this.mul = function(){
  9. res = 1;
  10. loop('mul', res);
  11. }
  12. function loop(method, res){
  13. for(var i = 0; i < args.length; i++){
  14. var item = args[i];
  15. if(method === 'add'){
  16. res += item;
  17. }else if(method === 'mul'){
  18. res *= item;
  19. }
  20. }
  21. console.log(res);
  22. }
  23. }
  24. var am1 = new AddMul(1, 2, 3, 4);
  25. am1.add();
  26. am1.mul();

2. 写一个构造车的函数,可以设置车的品牌,颜色,排量写一个构造消费者的函数,可以设用户的名字,年龄,收入 通过选车的方法,实例化该用户喜欢的车,在设置车的属性

  1. function Car(opt){
  2. this.brand = opt.brand,
  3. this.color = opt.color,
  4. this.displacement = opt.displacement
  5. this.myCar = function(){
  6. console.log(`My car brand is ${this.brand}, color
  7. is ${this.color}, displacement is ${this.displacement}`);
  8. console.log(this.displacement);
  9. }
  10. }
  11. function Consumer(user){
  12. this.name = user.name,
  13. this.age = user.age,
  14. this.earning = user.earning,
  15. this.choiceCar = function(){
  16. return new Car(user.opt);
  17. }
  18. }
  19. var c1 = new Consumer({
  20. name: 'xiaomeng',
  21. age: 16,
  22. earning: '30K',
  23. opt:{
  24. brand: 'benz',
  25. color: 'white',
  26. displacement: 3.0
  27. }
  28. });
  29. var car = c1.choiceCar();
  30. console.log(car);
  31. car.myCar();