继承之es5对比es6
2023-09-14 08:58:56 时间
es5 写法:
function Person(name){ this.name = name; } Person.prototype.sayName = function() { alert(this.name); } function VipPerson(name,level){ Person.call(this,name); this.level = level; } // 组合继承(缺点是prototype这一层多了一些无用的undefined属性) // VipPerson.prototype = new Person(); // VipPerson.prototype.constructor = VipPerson; //重写construtor // 寄生组合继承 VipPerson.prototype = Object.create(Person.prototype, { constructor: { //重写construtor value: VipPerson, enumerable: false, configurable: true, writable: true } }) var vipPerson = new VipPerson('amie',3);
es6的写法:
相比于es5,es6的写法简化了很多,extends、super就可以完成上面寄生组合继承的效果
super
,可以看成 Person.call(this, name)
。
class Person{ constructor(name){ this.name = name; } sayName(){ alert(this.name); } } class VipPerson extends Person{ constructor(name,level){ super(name); this.level = level; } } var person2 = new VipPerson('rick',3)
相关文章
- 浅聊:ES6模板字符串与一般字符串
- es6数组常用函数方法
- es6数组处理方法整理
- ES6数组新方法[通俗易懂]
- ES6基础之——继承extends
- 每天3分钟,重学ES6-ES12(十五)异步代码处理方案
- 每天3分钟,重学ES6-ES12(十四)asyncu002Fawait
- es6的map和set的区别_sleep和wait的区别
- JavaScript——ES6新增语法特性
- ES6面试点-WeakMap与Map的区别
- ES6常用数组方法总结(max,contant,some,every,filter,reduce,forEach,map)
- ES6从入门到精通-day01
- ES6 学习笔记(三)原始值与引用值
- es6扩展运算符、concat方法合并多个数组
- ES6内置对象Proxy代理数据demo
- ES6中的Iterator 和for of循环
- JS模块化—CJS&AMD&CMD&ES6-前端面试知识点查漏补缺_2023-03-13
- webpack原理(2):ES6 module在Webpack中如何Tree-shaking构建
- JavaScript语言基础-变量声明var和let和const(ES6特性)详解编程语言
- ES6 spread operator 展开运算符详解编程语言