我下面的代碼是這樣的:如果他沒有名字,是什麼使構造函數的object.prototype?
var triangle = {a:1, b:2, c:3};
function constructorFunction() {
this.color = "red";
}
constructorFunction.prototype = triangle;
我知道原型關鍵字在這句法擴展類:Object.prototype.method = function() {}
但什麼是這個例子嗎?在constructorFunction.prototype
之後沒有屬性或方法名稱,這裏會發生什麼?
不,這有點不同。 –
'prototype'不是關鍵字。 '.prototype'是'constructorFunction'的一個簡單屬性。 – Bergi
'.prototype'只能與'new'運算符一起使用。 要在不使用'.prototype'的情況下設置_real_ [[[prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain#Inheritance_with_the_prototype_chain)]](內部鏈接,\ __ proto__)使用'Object.create(real_prototype)'或'Object.setPrototypeOf(existing_object,new_real_prototype)' – befzz