我潛水深入到JavaScript和學習構造函數方法是如何工作的。如何更改對象的構造函數?
在下面的代碼,我希望,我將能夠覆蓋一個對象的構造函數,使新創建的實例將使用新的構造函數。但是,我似乎無法讓新實例使用新的構造函數。
任何有識之士爲正在發生的事情,將不勝感激!
function constructorQuestion() {
alert("this is the original constructor");
};
c = new constructorQuestion();
constructorQuestion.constructor = function() { alert("new constructor");}
howComeConstructorHasNotChanged = new constructorQuestion();
這裏的小提琴:http://jsfiddle.net/hammerbrostime/6nxSW/1/
忘了加上小提琴:http://jsfiddle.net/hammerbrostime/6nxSW/1/ – hammerbrostime