[參考 Javascript面向?qū)ο缶幊蹋ㄈ悍菢?gòu)造函數(shù)的繼承](http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html)
一、什么是"非構(gòu)造函數(shù)"的繼承?
請(qǐng)問怎樣才能讓"醫(yī)生"去繼承"中國(guó)人",也就是說,我怎樣才能生成一個(gè)"中國(guó)醫(yī)生"的對(duì)象?這里要注意,這兩個(gè)對(duì)象都是普通對(duì)象,不是構(gòu)造函數(shù),無法使用構(gòu)造函數(shù)方法實(shí)現(xiàn)"繼承"。
比如,現(xiàn)在有一個(gè)對(duì)象,叫做"中國(guó)人"。
var Chinese = {
nation:'中國(guó)'
};
var Doctor ={
career:'醫(yī)生'
}
二、object()方法
json格式的發(fā)明人Douglas Crockford,提出了一個(gè)object()函數(shù),可以做到這一點(diǎn)。
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var Doctor = object(Chinese);
Doctor.career = '醫(yī)生';
alert(Doctor.nation); //中國(guó)