function Person(name, sex) { // 构造函数
// var person = new Object(); 创建一个新对象
this.name = name; // 添加对象属性
this.sex = sex;
}
Person.prototype.showName = function () { // 对象添加方法
alert(this.name);
};
Person.prototype.showSex = function () {
alert(this.sex);
};
//-------------------------------------
function Worker(name, sex, job) {
//this->new出来的Worker对象
//构造函数伪装 调用父级的构造函数——为了继承属性
Person.call(this, name, sex);
this.job = job;
}
//原型链 通过原型来继承父级的方法
//Worker.prototype=Person.prototype;
for (var i in Person.prototype) {
Worker.prototype[i] = Person.prototype[i];
}
Worker.prototype.showJob = function () {
alert(this.job);
};
var oP = new Person('zgz', '男'); // 创建对象
var oW = new Worker('zgz', '男', '打杂的');
oP.showName(); // 调用对象方法
oP.showSex();
oW.showName();
oW.showSex();
oW.showJob();
// 小程序常用面向对象方法
var o = {
name: "zhanggongze",
ege: 28,
worker: 'web前端',
printName: function () {
return this.name;
},
printEge: function () {
return this.ege;
},
printWorker: function () {
return this.worker;
},
printNEW: function () {
return this.printName() + this.printEge() + this.printWorker();
}
}
var printNEW = o.printNEW();
console.log(printNEW);