有關繼承
有關繼承
function Graph() {
this.vertices = [];
this.edges = [];
}
const g = new Graph();
g.hasOwnProperty('edges')
// true
Graph.prototype.c = 123
// 繼承父親的屬性會在 __proto__ 顯示
g.hasOwnProperty('c')
// false
g.__proto__.hasOwnProperty('c')
// true以上的解釋是
去定義的,而不是一開始的建構子
可看下面的範例
而使用Dog.prototype= DDog.prototype;也不會覆蓋原本的Dog建構子
避免將函式內的屬性設為name
使用prototype指定屬性後要用new出新物件後才可使用
delete可用來刪除函式中的方法
ES5 With Object.create
Last updated