一、对象
- 在对象里的function叫做方法
- 在对象外的function叫做函数
1.1、对象的基本形式及增删改查
- 如果对象里面有这个属性名,那么对象.属性就属于修改;没有这个属性名则属于新增
在对象里面,this指向的就是当前对象本身
// 对象的基本形式var teacher = {name: '张三',sex: 'male',age: 18,weight: 130,teach: function(){console.log('I am teach JavaScript');},eat: function(){this.weight++;console.log(this.weight);console.log('I am having a dinner');},smoke: function(){this.weight--;console.log(this.weight);console.log('I am smoking');}}teacher.teach();teacher.eat();teacher.smoke();teacher.smoke();// 对象的增删改查// 如果对象里面有这个属性名,那么对象.属性就属于修改;没有这个属性名则属于新增// 在对象里面,this指向的就是当前对象本身// 新增teacher.height = 175;// 删除 需要借助关键词deletedelete teacher.age;// 修改teacher.age = 22;// 查询var res = teacher.weight;console.log(res);console.log(teacher);
二、简单了解数组里的两个常用方法
ary.splice(n, m); 从索引n开始删除m个
ary.indexOf(n); 找到n在数组里出现的索引位置,如果该数组不包含n,那么返回的结果是-1
// ary.splice(n, m);var ary = [1, 2, 3, 4, 5];ary.splice(0, 2);console.log(ary);// ary.indexOf(n);var arr = [11, 22, 33, 44, 55];var res = arr.indexOf(55);console.log(res);
三、对象字面量和构造函数
3.1、对象字面量
// 对象字面量var person = {name: 'liangyu',age: 18,weight: 130,height: 170}person.name = 'kola';console.log(person);
3.2、系统自带的构造函数
系统自带的构造函数和对象字面量没什么区别
// 构造函数 系统自带的构造函数var obj = new Object();obj.name = '张三';obj.age = 10;console.log(obj);
3.3、自定义构造函数
注意:自定义构造函数需要使用大驼峰来命名
- 构造函数如果不被执行,里面的this根本没有作用
构造函数实例化过程中,构造函数只有new了以后,里面的this才会执行当前这个实例化对象本身;如果不被new,把它当做普通函数执行的话,里面的this就指向window
// 自定义构造函数function Teacher(){this.name = '李四';this.age = '18';this.weight = 130;this.course = 'JavaScript';}var t1 = new Teacher();var t2 = new Teacher();console.log(t1, t2);// 代码优化function Teacher(name, age, weight, course){this.name = name;this.age = age;this.weight = weight;this.course = course;}var t1 = new Teacher('liangyu', 18, 130, 'JavaScript');var t2 = new Teacher('kola', 22, 120, 'HTML');console.log(t1, t2);// 代码再次优化: 终极写法function Teacher(opt){this.name = opt.name;this.age = opt.name;this.weight = opt.weight;this.course = opt.course;}var t1 = new Teacher({name: '张三',age: 19,weight: 120,course: 'Java'});var t2 = new Teacher({name: '李四',age: 23,weight: 130,course: 'CSS'});console.log(t1, t2);
四、作业
写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能
function compute(){var res = 0;var operation = {add: function(){for(var i = 0; i < arguments.length; i++){var item = Number(arguments[i]);res += item;}return res;},mul: function(){if(res == 0){res += 1;}for(var i = 0; i < arguments.length; i++){var item = Number(arguments[i]);res *= item;}return res;}}return operation;}var compute = compute();// console.log(compute.add('1', null, 3, 4));console.log(compute.mul(1, 2, 3, 4));
更新版 ```javascript // 作业
// 写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能// 普通写法function Compute(){var res = 0;this.plus = function(){loop(arguments, 'add', res);}this.mul = function(){res = 1;loop(arguments, 'mul', res);}function loop(args, methods, reds){for(var i = 0; i < args.length; i++){var item = args[i];if(methods === 'add'){res += item;}else if(methods === 'mul'){res *= item;}}console.log(res);}}var compute = new Compute();compute.plus(2, 4, 6);compute.mul(3, 5, 7);
// 构造函数版本function Compute(){var res = 0;this.plus = function(){Compute.prototype.loop(arguments, 'add', res);}this.mul = function(){res = 1;Compute.prototype.loop(arguments, 'mul', res);}}Compute.prototype = {loop: function(args, methods, res){for(var i = 0; i < args.length; i++){var item = args[i];if(methods === 'add'){res += item;}else if(methods === 'mul'){res *= item;}}console.log(res);}}var compute = new Compute();compute.plus(2, 4, 6);compute.mul(3, 5, 7);
- 写一个构造函数,可以设置车的品牌、颜色、及排量;再写一个消费者构造函数,设置用户的名字,年龄、收入,通过选择的方法实例化用户喜欢的车、再设置车的属性```javascriptfunction Car(opt){this.brand = opt.brand;this.color = opt.color;this.pt = opt.pt;};function Consumer(opt){this.name = opt.name;this.age = opt.age;this.income = opt.income;};function chooseCar(){var consumer = new Consumer({name: '小明',age: 18,income: '12k'});if(consumer.income < '10k'){var car = new Car({brand: '比亚迪',color: '白色的',pt: 1.5});console.log('您应该购买:' + (car.color + car.brand + '排量为:' + car.pt));}if(consumer.income > '10k'){var car = new Car({brand: '大众',color: '黑色的',pt: 2.0});console.log('您应该购买:' + (car.color + car.brand + '排量为:' + car.pt));}}chooseCar();
作业修正
// 作业// 写一个构造函数,可以设置车的品牌、颜色、及排量;再写一个消费者构造函数,设置用户的名字,年龄、收入,通过选择的方法实例化用户喜欢的车、再设置车的属性function Car(opt){this.brand = opt.brand;this.color = opt.color;this.displacement = opt.displacement;}function Person(opt){this.name = opt.name;this.age = opt.age;this.income = opt.income;this.selectCar = function(){var myCar = new Car(opt.carOpt);console.log(this.name + '挑选了一辆排量为' + myCar.displacement + '的' + myCar.color + myCar.brand);}}var jone = new Person({name: '约翰',age: 20,income: '10k',carOpt: {brand: '马自达',color: '黑色',displacement: '2.0'}});jone.selectCar();
