8.1对象
对象里的函数叫方法methods
外部的函数就叫函数funciton
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.address = '北京';//添加
teacher.drink = function(){
console.log('I am drinking beer');
}
teacher.drink();
teacher.teach = function(){//修改
console.log('I am teaching HTML');
}
delete teacher.address;//删除
delete teacher.teach;
console.log(teacher);
在对象里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);
}
},
classOver: function(){
this.students = [];
console.log('已下课');
}
}
attendance.join('张三');
attendance.join('李四');
attendance.leave('张三');
8.2构造函数及实例化
用系统内自带的构造函数
(对象是通过实例化构造函数而得出的对象实例)
var obj = new Object();//和对象字面量相等
obj.name = '张三';
obj.sex = '男士';
自定义构造函数
大驼峰
function Teacher() { // 构造工厂
this.name = '张三'; // 构造函数执行之前,this 没有生成
this.sex = '男';
this.weight = 130;
this.smoke = function(){
this.weight--;
console.log('smoke', this.weight);
}
this.eat = function(){
this.weight++;
console.log('eat dinner', this.weight);
}
}
// 只有 new 构造函数了之后才会有实例对象 teacher,这时候才存在this,this谁用就指向谁
var t1 = new Teacher();
var t2 = new Teacher();
t1.name = '李四';
t1.smoke();
t1.smoke();
console.log(t1, t2);
传参方式
function Teacher(opt) {
this.name = opt.name;
this.sex = opt.sex;
this.weight = opt.weight;
this.smoke = function(){
this.weight--;
console.log('smoke', this.weight);
}
this.eat = function(){
this.weight++;
console.log('eat dinner', this.weight);
}
}
var t1 = new Teacher({name: '张三', sex: '男', weight: 145});
var t2 = new Teacher({name: '李四', sex: '女', weight: 90});
console.log(t1, t2);
写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能
// 方法1
function Compute(){
var args = arguments,
res;
this.add = function(){
res = 0;
loop('add', res);
}
this.multiply = 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 compute = new Compute(3, 4, 5);
compute.add();
compute.multiply();
// 方法2
function Compute(){
var res = 0;
this.add = function(){
loop(arguments, 'add', res);
}
this.multiply = function(){
res = 1;
loop(arguments, 'mul', res);
}
function loop(args, 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 compute = new Compute();
compute.add(1, 2, 3, 4, 5);
compute.multiply(8, 5);
写一个构造车的函数,可设置车的品牌,颜色,排量,写一个构造消费者的函数,设置用户的名字,年龄,收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性
function Car(opt) {
this.brand = opt.brand;
this.color = opt.color;
this.consumption = opt.consumption;
}
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.consumption + '的' + myCar.color
+ myCar.brand);
}
}
var jone = new Person({
name: 'Jone',
age: 28,
income: '20k',
carOpt: {
brand: '马自达',
color: 'white',
consumption: '2.0'
}
});
jone.selectCar();
console.log(jone);