1. 对象

1.1 增删改查

  1. var teacher = {
  2. height: 180;
  3. weight: 130;
  4. teach: function(){
  5. console.log('I am teaching JS');
  6. },
  7. eat: function(){
  8. console.log('I am having a dinner);
  9. }
  10. }
  11. teacher.address = '北京'; //增
  12. teacher.height = 155; //改
  13. delete teacher.height; //删
  14. delete teache.teach; //删

1.2 数组方法

  1. 1.arr.indexOf();//找数组下标
  2. 2.arr.splice(x,y);//从数组x位删除y个值
  3. 3.arr.push(x);//从数组最后一位添加x值

2. 构造函数

2.1 构造函数

  1. /*1.对象字面量、对象直接量*/
  2. var obj = {
  3. name: '张三',
  4. sex: 'male'
  5. }
  6. /*2.系统自带的构造函数 -> 较少使用*/
  7. var obj = new Object();//与对象字面量相等
  8. obj.name = '张三';
  9. obj.sex = '男';
  10. console.log(obj);
  11. /*
  12. 3.自定义构造函数 -> 大量使用
  13. 大驼峰
  14. */
  15. function Teacher(){
  16. this.name = '张三';
  17. this.sex = '男士';
  18. this.somke = function(){
  19. console.log('I am smoking');
  20. }
  21. }
  22. var teacher = new Teacher();

3. 作业

  1. /*
  2. 1.写一个构造函数,接受数字类型的参数,参数数量不定,完成参数相加和相乘的功能
  3. */
  4. function Compute() {
  5. var res = 0;
  6. this.add = function () {
  7. loop(arguments, "add", res);
  8. };
  9. this.mul = function () {
  10. res = 1;
  11. loop(arguments, "mul", res);
  12. };
  13. function loop(args, method, res) {
  14. for (var i = 0; i < args.length; i++) {
  15. var item = args[i];
  16. if (method === "add") {
  17. res += item;
  18. } else if (method === "mul") {
  19. res *= item;
  20. }
  21. }
  22. console.log(res);
  23. }
  24. }
  25. var compute = new Compute();
  26. compute.add(2, 4, 6);
  27. compute.mul(2, 4, 6);
  28. /*
  29. 2.写一个构造车的函数,可设置车的品牌、颜色、排量
  30. 再写一个构造消费者的函数,设置用户的名字、年龄、收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性
  31. */