一、对象
// 字面量var teacher = {name: '张三',age: 32,sex: 'male',height: 176,weight: 130,teach: function () {//就是函数,在对象中不能叫函数了,叫方法console.log('I am teaching JavaScript');},smoke: function () {console.log('I am smoking');},eat: function () {console.log('I am having a dinner');}}// 增加属性/方法teacher.email = '350604967@qq.com';teacher.drink = function () {console.log('I am drinking');}// 修改属性/方法teacher.age = '25';teacher.teach = function () {console.log('I am teaching HTML');}// 删除属性/方法delete teacher.name;delete teacher.eat;console.log(teacher);//--------------------------------------------------------------------------
var teacher = {name: "张三",age: 32,sex: "male",height: 176,weight: 130,teach: function () {//就是函数,在对象中不能叫函数了,叫方法console.log("I am teaching JavaScript");},smoke: function () {teacher.weight--;console.log(teacher.weight); //正确 129 128 129// console.log(weight); //错误Uncaught ReferenceError: weight is not defined// console.log(this.weight); // 正确与teacher.weight一样},eat: function () {teacher.weight++;console.log(teacher.weight);},};teacher.smoke();teacher.smoke();teacher.eat();
this指代对象本身
对象function函数,才有this
字面量是对象,有this,里面的函数有this,变量可以接收this,因为attendance是对象,它里面就有this
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() {this.students = [];console.log('已下课');}}attendance.join('张三');attendance.join('李四');attendance.join('王五');attendance.join('赵六');attendance.join('小红');attendance.join('小明');attendance.leave('王五');attendance.classOver();
二、构造函数
1、自定义构造函数 - 大驼峰命名
2、通过 new 关键字创建实例化对象
3、每次 new 创建的对象都是不同的对象
4、this 指向实例化对象
自带构造函数
var obj=new Object();//与对象字面量相等obj.name='张三';obj.sex='男士';console.log(obj)
自定义构造函数
只靠系统自带的构造函数是不够的,功能满足不了需求,需要自己定义、定制构造函数
对象字面量是键值对,所以是:,自定义函数是代码语句是=号,赋值号。
this指向的是谁,是对象,不是构造函数。对象是new出来的。
没有new出来对象,在构造函数Teacher中name、sex都不存在,因为不是var name = ‘张三’; 不是声明语句,在AO中name、sex都不存在。
构造函数实例化对象。
function Teacher() {this.name = '张三';this.sex = '男';this.weight = 130;this.smoke = function () {this.weight--;console.log(this.weight);}this.eat = function () {this.weight++;console.log(this.weight);}}var t1 = new Teacher();var t2 = new Teacher();t2.name = '李四';t1.smoke(); // 129t1.smoke(); // 128t2.smoke(); // 129console.log(t1)//Teacher:objectconsole.log(new Teacher().sex)//男console.log(Teacher)//func Teacherconsole.log(Teacher.name)//Teacherconsole.log(Teacher.sex)//undefined
三、实例化
function Teacher() {this.name = "张三";this.sex = "男";this.weight = 130;this.smoke = function () {this.weight--;console.log(this.weight);};this.eat = function () {this.weight++;console.log(this.weight);};}var teacher1=new Teacher();var teacher2=new Teacher();teacher1.name='李四';console.log(teacher1,teacher2)

teacher1、teacher2是不同的对象,new出来的对象都是完全不同的,新的对象
改变一个对象对另一个对象没有影响
//初始化构造函数function Teacher(name, sex, weight, course) {this.name = name;this.sex = sex;this.weight = weight;this.course = course;}var t1 = new Teacher('李四', '女', 150, 'HTML');console.log(t1);
实例化对象时就给其属性赋值,不用实例化之后在赋值
function Teacher(opt) {this.name = opt.name;this.sex = opt.sex;this.weight = opt.weight;this.course = opt.course;this.smoke = function () {this.weight--;console.log(this.weight);}this.eat = function () {this.weight++;console.log(this.weight);}}var t1 = new Teacher({name: '张三',sex: '男',weight: 145,course: 'JS'})
传一整个对象比传多个参数好
多个参数不知道参数的具体意义,对顺序有要求
vue.js就是这么写的,配置的
四、作业
写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能
function Compute() {var length = arguments.length;var sum = 0;var mul = 1;for (var i = 0; i < length; i++) {sum += arguments[i];mul *= arguments[i];}this.add = function () {console.log(sum);}this.mul = function () {console.log(mul);}}var c1 = new Compute(1, 2, 3, 4);c1.add();c1.mul();console.log(nn.length)//undefined
不用this,用var声明,外部不能调用
function Compute(){var args=arguments;//保存Compute函数的arguments,供别的函数使用,要不add的arguments是从ys.add(1,2,3,4)传入的this.add=function() {sum=0;for (var i = 0; i <args.length; i++){sum += args[i];}console.log(sum)}this.mul=function() {var mul=1;for (var i = 0; i <args.length; i++){mul*=args[i]}console.log(mul)}}var ys=new Compute(1,2,3,4)ys.add()ys.mul()
mul也需要循环一遍
function Compute(){var args=arguments;this.add=function() {// sum=0;// for (var i = 0; i <args.length; i++){// sum += args[i];// }// console.log(sum)console.log(ys(args,"+",0))}this.mul=function() {// var mul=1;// for (var i = 0; i <args.length; i++){// mul*=args[i]// }// console.log(mul)console.log(ys(args,"*",1))}function ys(args,symbol,res) {for (var i = 0; i < args.length; i++){if(symbol=='+'){res+=args[i]}else if (symbol =='*'){res *= args[i]}}return res;}}var ys=new Compute(1,2,3,4)ys.add()ys.mul()ys.add()
写一个构造车的函数,可设置车的品牌,颜色,排量;再写一个构造消费者的函数,设置用户的名字,年龄,收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性
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.caropt = opt.caropt;this.select = function (caropt) {return new Car(caropt)}this.mycar = this.select(this.caropt);}var p1 = new Person({name: '张三',age: 25,income: '15K',caropt: {brand: '奔驰',color: 'red',displacement: 100}});console.log(p1.mycar);
function Car(opt){this.brand=opt.brand ;this.color=opt.color;this.delivery=opt.delivery;}function Consumer(opt){this.name=opt.name;this.age=opt.age;this.income=opt.income;this.selectCar = function(caropt) {return new Car(caropt)};}
function Car(opt){this.brand=opt.brand ;this.color=opt.color;this.delivery=opt.delivery;}function Consumer(opt){this.name=opt.name;this.age=opt.age;this.income=opt.income;// this.caropt=opt.caropt;// this.selectCar = function(caropt) {// return new Car(caropt)// };// this.myCar=this.selectCar(this.caropt)this.selectCar=function() {return new Car(opt.caropt);}}var consumer=new Consumer({name:'enjoy',age:18,income:10000,caropt: {brand:'benz',color:'red',delivery:2.0}})console.log(consumer.selectCar())
function Car(opt){this.brand=opt.brand ;this.color=opt.color;this.delivery=opt.delivery;}function Consumer(opt){this.name=opt.name;this.age=opt.age;this.income=opt.income;this.caropt=opt.caropt;this.selectCar = function(caropt) {return new Car(caropt)};this.myCar=this.selectCar(this.caropt)}var consumer=new Consumer({name:'enjoy',age:18,income:10000,caropt: {brand:'benz',color:'red',delivery:2.0}})console.log(consumer)
