一、es5里面的类
1、最简单的类
function Person() { this.name = '张三'; // 实例化属性 this.age = 20; this.run = function () { // 实例化方法,必须new才能调用 alert(this.name + '在运动'); }}var p = new Person(); // 实例化构造函数alert(p.name);
2、构造函数和原型链里面增加方法
注:原型链上面的属性会被多个实例共享,构造函数不会Person.prototype.sex = "男";Person.prototype.work = function () { alert(this.name + '在工作');}var p = new Person();p.work();
3、类里面的静态方法
function Person() { this.name = '张三'; // 实例化属性 this.age = 20; this.run = function () { // 实例化方法,必须new才能调用 alert(this.name + '在运动'); }}Person.getInfo = function () { alert('我是静态方法');}Person.getInfo(); // 调用静态方法
4、es5里面的继承,对象冒充实现继承
function Person() { this.name = '张三'; // 实例化属性 this.age = 20; this.run = function () { // 实例化方法,必须new才能调用 alert(this.name + '在运动'); }}Person.prototype.sex = "男";Person.prototype.work = function () { alert(this.name + '在工作');}注:Web类继承Person类,原型链+对象冒充的组合继承模式function Web() { Person.call(this); /* 对象冒充实现继承 */}var w = new Web();w.run(); // 对象冒充可以继承构造函数里面的属性和方法,但是没法继承原型链上面的属性和方法
5、es5里面的继承,原型链实现继承
function Person() { this.name = '张三'; // 实例化属性 this.age = 20; this.run = function () { // 实例化方法,必须new才能调用 alert(this.name + '在运动'); }}Person.prototype.sex = "男";Person.prototype.work = function () { alert(this.name + '在工作');}// Web类继承Person类,原型链+对象冒充的组合继承模式function Web() {}Web.prototype = new Person(); //原型链实现继承var w = new Web();// 原型链实现继承:可以继承构造函数里面的属性和方法 也可以继承原型链上面的属性和方法var w = new Web();w.run();
6、原型链实现继承的问题
function Person(name, age) { this.name = name; this.age = age; this.run = function () { alert(this.name + '在运动'); }}Person.prototype.sex = "男";Person.prototype.work = function () { alert(this.name + '在工作');}var p = new Person('李四', 20);p.run();// 问题来了function Web(name, age) {}Web.prototype = new Person();var w = new Web('赵四', 20); // 实例化子类的时候没法给父类传参w.run(); // this.name 是undefined
7、原型链+对象冒充的组合继承模式(常用)
function Person(name, age) {
this.name = name;
this.age = age;
this.run = function () {
alert(this.name + '在运动');
}
}
Person.prototype.sex = "男";
Person.prototype.work = function () {
alert(this.name + '在工作');
}
// 重点在这里
function Web(name, age) {
Person.call(this, name, age); // 对象冒充继承,实例化子类可以给父类传参
}
Web.prototype = new Person();
var w = new Web('赵四', 20); //实例化子类的时候没法给父类传参
w.run();
8、原型链+对象冒充继承的另一种方式
function Person(name, age) {
this.name = name;
this.age = age;
this.run = function () {
alert(this.name + '在运动');
}
}
Person.prototype.sex = "男";
Person.prototype.work = function () {
alert(this.name + '在工作');
}
function Web(name, age) {
Person.call(this, name, age); //对象冒充继承 可以继承构造函数里面的属性和方法、实例化子类可以给父类传参
}
// 这里直接继承原型链
Web.prototype = Person.prototype;
var w = new Web('赵四', 20); //实例化子类的时候没法给父类传参
w.run();