ES6
ES5
ES6
var Animal = function(){};
var dog = new Animal();
dog.__proto__.__proto__.__proto__; // null
var text = 'some text here';
text.split(' ');
// ['some', 'text', 'here']
String.prototype.split
// split() { [native code] }
var Animal = function(name){
this.name = name
};
Animal.prototype.color = 'yellow'
var dog = new Animal('dog');
dog.color;
// 1. 先找 dog.color 得到 undefined
// 2. 在找 dog.prototype.color 得到 "yellow"
dog.nonexistence;
// 1. 先找 dog.color 得到 undefined
// 2. 在找 dog.prototype.nonexistence
// 3. 得到 "undefined"
ES6
Object.getPrototypeOf(dog) === dog.__proto__;
// true
Object.setPrototypeOf(obj, property);
dog.__proto__ === Animal.prototype;
// true
var MyClass = function(){};
MyClass.prototype.foo = 10;
var myObj1= new MyClass();
myObj1.foo; // 10
MyClass.prototype.foo = 20;
myObj1.foo; // 20
function Animal(name){
this.name = name;
}
Animal.prototype.run = function(){
console.log(this.name + "is running!!");
}
var a = new Animal("a");
var b = new Animal("b");
Animal.prototype //Animal {}
Animal.prototype instanceof Object // true
Animal.prototype.constructor == Animal) // true
a.__proto__ == Animal.prototype //true
b.__proto__ == Animal.prototype //true
a.__proto__.__proto__ // Object {}
a.__proto__.run == a.run //true
a.__proto__.run == a.prototype.run // true
ES5
var Animal = function(name){
this.name = name;
};
Animal.prototype.run = function(){
console.log(this.name + ' is running');
}
function Dog(name){
// 調用父類的構造函數,通過改變this指向將屬性賦值到新的實例對象
Animal.call(this, name);
}
Dog.prototype = new Animal();
var dog = new Dog("dog");
dog.run(); // dog is running!!
var shape = {
width: 100,
height: 100,
getArea: function(){
return this.width * this.height
}
};
var rectangle = Object.create(shape);
// rectangle 是由 shape 繼承來的物件
rectangle.width = 50;
rectangle.getArea(); // 5000
// 呼叫 getArea 時裡面的 this 指向 rectangle
rectangle.__proto__ === shape; // true
rectangle; // {width: 50}
shape;
// shape -> Object.prototype -> null
rectangle
// rectangle -> shape -> Object.prototype -> null
for(var i in shape) {
console.log(i);
}
// width
// height
// getArea
var rectangle = Object.create(shape);
rectangle.width = 50; // rectangle 只有 width 這個屬性
for(var i in rectangle) {
console.log(i);
}
// width
// height
// getArea
var rectangle = Object.create(shape);
rectangle.width = 50; // rectangle 只有 width 這個屬性
for(var i in rectangle) {
if(rectangle.hasOwnProperty(i)) console.log(i)
}
// width
var keys = Object.keys(rectangle);
keys.forEach(function(key, idx){
console.log(key);
});
// width
var shape = {};
Object.defineProperties(shape, {
'width': {
value: 100,
writable: true,
enumerable: false,
},
'height': {
value: 100,
writable: true,
enumerable: false,
}
// etc.
});
var rectangle = Object.create(shape);
for(i in rectangle) {
console.log(i);
}
// nothing print out
Array.prototype.sum = function(){
var sum = this.reduce(
(sum, current) => sum + current
, 0);
return sum;
};
var ary = [1, 2, 3];
ary.sum(); // 6
function Dog(name){
Animal.call(this, name);
}
Dog.prototype = new Animal();
var dog = new Dog("dog");
dog.__proto__;
// Animal {name: undefined}
// dog 的 prototype 多了不必要的屬性 "name"
function Dog(name){
Animal.call(this, name);
}
Dog.prototype = new Animal();
var dog = new Dog("dog");
dog.constructor === Animal; // true
dog.constructor === Dog; // false
var F = function(){};
F.prototype = Animal.prototype;
Dog.prototype = new F();
Dog.prototype.constructor = Dog;
function objCreate(prototype){
var F = function(){};
F.prototype = prototype;
return new F();
}
function inherit(subclass, parentclass){
subclass.prototype = objCreate(parentclass.prototype);
subclass.prototype.constructor = subclass;
}
function Animal(name){
this.name = name;
}
Animal.prototype.run = function(){
console.log(this.name + "is running!!");
}
function Dog(name){
Animal.call(this,name);
}
inherit(Dog, Animal);
var dog = new Dog('dog');
dog.run();//dog is running!!
ES6
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
const p = new Polygon(100,100);
// Polygon {height: 100, width: 100}
var p = new Polygon(); // ReferenceError
class polygon {}
// unnamed
var Polygon = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Polygon.name);
// "Polygon"
// named
var Polygon = class Polygon2 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Polygon.name);
// "Polygon2"
class Rectangle extends Polygon {
// do something here...
}
class Rectangle extends Polygon {
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea(){
return this.height * this.width;
}
}
const square = new Rectangle(100, 100);
console.log(square.area);
// 10000
class Polygon{
constructor(w, h){
this.w = w;
this.h = h;
}
getShape(){
return "polygon"
}
}
class Rectangle extends Polygon{
getShape(){
return "Rectangle"
}
}
const square = new Rectangle(100, 100);
square.getShape(); // "Rectangle"
class Rectangle extends Polygon{
getShape(){
const rs = super.getShape();
return `quadrilateral of ${rs}`;
}
}
const square = new Rectangle(100, 100);
square.getShape();
// "quadrilateral of polygon"
class Triangle extends Polygon{
constructor(sideA, sideB, sideC){
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
}
var t = new Triangle();
// ReferenceError: Must call super constructor in derived class
class Triangle extends Polygon{
constructor(sideA, sideB, sideC){
super(0 ,0);
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
}
var t = new Triangle(3, 4, 5);
// Triangle {w: 0, h: 0, sideA: 3, sideB: 4, sideC: 5}
class Point {
constructor(x, y){
this.x = x;
this.y = y;
}
static distance(p1, p2){
const dx = p1.x - p2.x;
const dy = p1.y - p2.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(0, 0);
const p2 = new Point(3, 4);
Point.distance(p1, p2); // 5
class Polygon{
constructor(w, h){
this.w = w;
this.h = h;
}
static w = 10;
}
const rect = new Polygon(100, 100);
rect.w; // 100;
Polygon.w; // 10
class MyClass{};
MyClass.prototype.foo = 100;
var c1 = new MyClass();
c1.foo; // 100
var c2 = new MyClass();
c2.__proto__.foo = 200;
c1.foo; // 200
當你想要在 prototype 上建立共用的 primitive 屬性,目前只能這樣做
當 這份草案通過就可以改成這樣
class MyClass{
foo = 100;
};
var c1 = new MyClass();
c1.foo; // 100
請修改下面的物件,當它被設定屬性 width 時
如果數值不是整數,會四捨五入
如果輸入的數值非法 (包含負數),會變成 -1
var shape = {
width: 0,
};
shape.width = 3.4;
console.log(shape.width); // 3
shape.width = -infinity;
console.log(shape.width); // -1
// hint
// Object.defineProperty()
承上題,請改用 class 方式達成
class Shape {
// ...
}
var shape = new Shape();
shape.width = 3.4;
console.log(shape.width); // 3
// hint: get set