剛接觸js,看到一個prototype的例子,但里面其中一段代碼有些疑問,代碼如下:function a()
{ this.showMsg = function()
{
alert("a want showMsg");
}
}
a.showMsg = function()
{
alert("a want showMsg static");
} var p = new a();
a.showMsg(); //輸出a want showMsg static
p.showMsg(); //輸出a want showMsg一個是在函數里定義了屬性,一個是函數外部定義的屬性。請問兩種方式的本質以及什么情況下會用到?JS看的頭暈腦脹,各種繼承當時看懂了結果隔一天又忘,看多了感覺又攪在一塊,接下來準備繼續學習DOM,哎,腦子好亂。
剛學JS不久的新人關于函數中屬性定義的疑問
繁星點點滴滴
2018-10-18 13:10:03