JS prototype作用
阿新 • • 發佈:2018-11-09
prototype可檢視原型屬性,還可對原型新增屬性或方法
function Car(name) { this.name = name; this.run = function () { console.log(this.height+'cm '+this.name + 'is run!') } } var dazhong = new Car('dazhong'); Car.prototype.height = null; //給物件新增新屬性 dazhong.height = 200 //給屬性賦值 dazhong.run(); //呼叫run方法列印 console.log(Car.prototype) //prototype不僅能在原型物件上新增屬性或方法,還可檢視原型屬性
現在我們列印dazhong;
console.log(dazhong.prototype)
發現沒有prototype這個屬性,我們可以用__proto__這個非標準用法來檢視這個物件的屬性