
本文共 4052 字,大约阅读时间需要 13 分钟。
###������������������������ ES6 ���������
####���������������������
���������������������������������������������������prototype������������������������������������������������������������ toString ������������������������ prototype��������������������� Sergeant ������������������������������������������������������������������������������������������������������������������
#####������������������������������������ inheritPrototype
function inheritPrototype(subType, superType) { // ��������������������������������� const prototype = Object.create(superType.prototype); // ���������������������������������������������������������constructor������ prototype.constructor = subType; // ��������������������� subType.prototype = prototype;}
������������������������������������ Father������������������������������������������������ sayName���
Father ������������������
function Father(name) { this.name = name; this.colors = ["red", "blue", "green"];}
Father ���������������
Father.prototype.sayName = function() { alert(this.name);};
#####������������ Father���������������������������������������
��������������������������� Son������������������������������
Son ������������������
function Son(name, age) { Father.call(this, name); this.age = age;}
#####���������������������
inheritPrototype(Son, Father);
#####������������������
Son.prototype.sayAge = function() { alert(this.age);};
#####������������
const demo1 = new Son("TianTian", 21);const demo2 = new Son("TianTianUp", 20);// demo1.colors ������ ["red", "blue", "green"]// demo2.colors ������demo1.colors.push("2"); // ["red", "blue", "green", "2"]demo2.colors.push("3"); // ["red", "blue", "green", "3"]demo2.sayName(); // alert "TianTianUp"console.log(demo1 instanceof Son); // true
������������������������������������������������������������������������������������������������������������������ new
���������������������������������������������
####ES6 ���������
ES6 ��������������������������������������������������������������������������������������������������������� Rectangle ���������������������
#####Rectangle ���
class Rectangle { constructor(height, width) { this.height = height; this.width = width; } get area() { return this.calcArea(); } calcArea() { return this.height * this.width; }}
#####������������
const rectangle = new Rectangle(40, 20);// rectangle��� area ������������������������console.log(rectangle.area); // ������ 800
#####������ Square ��������� Rectangle ���
class Square extends Rectangle { constructor(len) { // ������������������ super ������������ super(len, len); this.name = "Square"; } get area() { return this.height * this.width; }}
#####������������
const square = new Square(20);console.log(square.area); //mina ��� 20 * 20 = 400
��������� Square ������������������ Rectangle ��� constructor ��������������������������� height ��� width��������������� len ������ length��������� Rectangle ��������������� area ������ Square ������������������������������������������������
#####������������������������������������������
��������������������������������������������������������������������������������������������������������������������������������������������������� ES6 ������������������������������������������������������������������������������������������������
���������������������������������������������������������������ES6 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
��������������������������������������������� JavaScript ���������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
