【1】最简单的类,构造函数
function Person(){
this.name = 'jack';
this.age = 18;
}
var p = new Person();//实例化一个方法
console.log(`${p.name}---${p.age}`);
【2】构造函数和原型链里面增加方法
//构造函数上面的属性不会被多个实例共享
function Person(){
this.name = 'jack';//定义属性
this.age = 20;
this.say = function(){//定义方法
alert('hi,everybody');
}
}
//原型链上的面的属性会被多个实例共享
Person.prototype.sex = '男';
Person.prototype.run = function(){
alert('I am running');
}
var p = new Person();//实例化一个方法
console.log(p.name);
console.log(p.age);
p.say();
console.log(p.sex);
p.run();
【3】类里面的静态方法
function Person(){
this.name = 'jack';
this.age = 20;
this.say = function(){
alert('say hello world');
}
}
Person.sex = '男';
Person.getInfo = function(){
alert('我是一个静态方法');
}
//调用静态的方法
console.log(Person.sex);
Person.getInfo();
【4】es5里面的继承 对象冒充实现继承
function Person(){
this.name = 'jack';
this.age = 20;
this.say = function(){
alert('say hello world');
}
}
Person.prototype.sex = '男';
Person.prototype.getInfo = function(){
alert('这是原型链上的一个方法');
}
//实现继承
function Web(){
Person.call(this);//Web 就继承了Person构造函数里的属性和方法
}
//Web类 继承Person类, 原型链+对象 冒充的组合继承模式
var web = new Web();
console.log(web.name);//对象冒充可以继承构造函数里面的属性和方法
console.log(web.age);
web.say();
//下面来试着继承Person 原型链里的属性和方法,看是否能成功
console.log(web.sex);//undefined
web.getInfo();//会报错,web.getInfo is not a function
//对象冒充可以继承构造函数里面的属性和方法,但是没法继承原型链上的属性和方法。
【5】 es5里,原型链的继承(既可以实现构造函数的继承又可以实现原型链的继承)
function Person(){
this.name = 'jack';
this.age = 20;
this.say = function(){
alert('say hello world');
}
}
Person.prototype.sex = '男';
Person.prototype.getInfo = function(){
alert('这是一个眼项链方法')
}
function Web(){}
Web.prototype = new Person();//原型链实现继承
//原型链实现继承:可以继承构造函数里面的属性和方法 也可以继承原型链上面的属性和方法。
var web = new Web();
console.log(web.name);
console.log(web.age);
web.say();
console.log(web.sex);
web.getInfo();
【6】es5中,原型链继承存在的问题(实例化子类没法给父类传值)
实例化的子类没法给父类传值
function Person(name,age){
this.name = name;
this.age = age
this.say = function(){
alert('say hello world');
}
}
Person.prototype.sex = '男';
Person.prototype.getInfo = function(){
alert('这是一个原型链方法');
}
// var p = new Person('jack',20);//可以传参
// console.log(p.name);//jack
// console.log(p.age);//20
function Web(name,age){}
Web.prototype = new Person();
var web = new Web('jack',20);//实例化的子类没法给父类传值
console.log(web.name);//undefined
console.log(web.age);//undefined
【7】原型链+构造函数的组合继承模式(可以实例化子类向父类传值)
function Person(name,age){
this.name = name;
this.age = age;
this.say = function(){
alert(this.name+' say hello world');
}
}
Person.prototype.sex = '男';
Person.prototype.getInfo = function(){
alert(this.name + '这是原型链里的方法');
}
function Web(name,age){
Person.call(this,name,age);//对象冒充继承,可以继承构造函数里面的属性和方法,实例化子类可以给父类传值
}
Web.prototype = new Person();
var web = new Web('jack',23);
console.log(web.name);
console.log(web.age);
web.say();
web.getInfo();
【8】原型链+构造函数的组合继承模式的另一种写法
function Person(name,age){
this.name = name;
this.age = age;
this.say = function(){
alert(this.name+' say hello world');
}
}
Person.prototype.sex = '男';
Person.prototype.getInfo = function(){
alert(this.name +'说这是一个原型链方法');
}
function Web(name,age){
Person.call(this,name,age);//对象冒充继承,可以实现 实例化子类给父类传值
}
Web.prototype = Person.prototype;
var web = new Web('jack',22);
console.log(web.name);
console.log(web.age);
web.say();
web.getInfo();