经过长达八周的编程刷怪,升级,以为有能力可以与boss切磋切磋,没想到还没出新手村就遇见隐藏boss,只能被按在地上摩擦,装备掉一地。面对boss的强有力的进攻,不能说的招招暴击吧,也能说是每次都能直击要害,已经被打的毫无“人性”。
JavaScript高级和ES6的学习计划逐渐步入尾声,但对与他们的学习还仍未结束,ES6和JavaScript的使用会遍历整个前端编程过程,我们都还在路上。在经历两周的在学习过程中,我也逐渐了解到,ES6和JavaScript的学习不能急与求成,否则就会忘了前面的,混了后面的。到最后啥都会一点,但有好像啥都能不会,使自己的学习效率最大化才是重要的。
下面是JavaScript中关于继承的方法(我一直以为是三种,没想到竟然有八种)
在此列举了五种
// 1.原型链继承
//创建父类型
let Parents = function(name,age){
this.name = name;
this.age = age;
}
//为父类型创建方法
Parents.prototype.setName = function(name){
this.name = name;
}
//创建子类型
let Chlid = function(price){
this.price = price;
}
//将父类型的实例化对象赋值给子类型的原型
Chlid.prototype = new Parents();
//改变子类型的原型
Chlid.prototype.constructor = Chlid;
//给子类型创建方法
Chlid.prototype.setPrice = function(price){
this.price = price;
}
//建立子类型的实例化对象
let chlid = new Chlid(20000);
//实例化对象可以调用父类型的方法
chlid.setPrice(1500);
chlid.setName('tom');
console.log(chlid.price,chlid.name);//1500 tom
//2. 盗用构造函数继承
let Person = function(name,age){
this.name = name;
this.age = age;
}
let Student = function(name,age,price){
//使用父类型中声明的变量
Person.call(this,name,age);
this.price = price;
}
//建立子类型的实例化对象
let student = new Student('bob',12,1500);
console.log(student.name,student.age,student.price);//bob 12 1500
//3.组合继承
//创建父类型
let numParent = function(name,age){
this.name = name;
this.age = age;
}
//为父类型创建方法
numParent.prototype.setName = function(name){
this.name = name;
}
let numChlid = function(name,age,price){
numParent.call(this,name,age);
this.price = price;
}
numChlid.prototype = new numParent();
numChlid.prototype.constructor = numChlid;
numChlid.prototype.setPrice = function(price){
this.price = price;
}
let numchlid = new numChlid('lisa',23,120000);
numchlid.setPrice(150000);
numchlid.setName('Jack');
console.log(numchlid.price,numchlid.age,numchlid.name);//150000 23 Jack
//4.原型式继承
// 根据已有的对象创建一个新的对象,同时不必创建新的自定义对象类型。
function createParents(name, age) {
this.name = name;
this.age = age;
}
createParents.prototype.setName = function (name) {
this.name = name;
}
function createChild(name, age, price) {
createParents.call(this, name, age);
this.price = price;
}
createChild.prototype = Object.create(createParents.prototype);
//改变子类型的原型
createChild.prototype.constructor = createParents;
let create = new createChild('图图', 8, 250)
create.setName('壮壮妈');
console.log(create.name);//壮壮妈
//5.类继承
//创建父类型
class classParents{
constructor(name,age){
this.name = name;
this.age = age;
}
//为父类型设置方法
setName(name){
this.name = name;
}
}
//进行类的继承
class classChild extends classParents{
constructor (name, age, price){
super(name,age);
this.price = price;
}
}
//为子类型创建实例化对象
let classchild = new classChild('沸羊羊',12,35);
//实例化对象可以调用父类型的方法
classchild.setName('美羊羊桑');
console.log(classchild.name,classchild.price);//美羊羊桑 35
在编程中不断升级,刷怪(敲代码)才是王道,在其过程中为自己不断地添加装备(学习方法和编程技巧)也是必不可少的,所以在以后的学习中也需要不断努力地肝,早日走出新手村。
前端冒险的第56天