ES5 与 ES6 继承内置对象的问题. 请各位来帮忙看下

拿好不谢 2017-05-04 09:50:06
在 ES5 中实现内置对象继承.结果......

function MyArray() {
Array.apply(this, arguments);
}

MyArray.prototype = Object.create(Array.prototype, {
constructor: {
value: MyArray,
writable: true,
configurable: true,
enumerable: true
}
});

var colors = new MyArray();
colors[0] = "red";
console.log(colors.length); // 0

colors.length = 0;
console.log(colors[0]); // "red"



在 ES6 中用 class 实现继承就没问题.


class MyArray extends Array {
// 空代码
}

var colors = new MyArray();
colorsp[0] = 'red';
console.log(colors.length); // 1

colors.length = 0;
console.log(colors[0]); // undefined



现在已知 :
1. 在 ES5 的传统继承中,this 的值会先被派生类创建,随后基类构造器才被调用.
2. 在 ES6 基于类的继承中,this 的值会先被基类创建,随后才被派生类的构造器所修改.


请问各位,这两句话怎么理解,或者说 怎么用代码表示出来. 理解这个问题的关键点在哪 ?
...全文
158 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
似梦飞花 2017-05-05
  • 打赏
  • 举报
回复
es6现有的父类的this 子类扩展父类 所以父类的所有内部的 不可遍历的所有的方法子类都有了 而es5相反 所以有些东西继承不过来

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧