前端开发 理解js中的原型链，prototype与__proto__的关系 登录   注册   发帖

### 理解js中的原型链，prototype与__proto__的关系

sk　 2017-2-15
0

 `1` `
 `2` ` var` `Person = ``function` `() { };`
 `3` ` var` `p = ``new` `Person();`
 `4` ``

<1> var p={}; 也就是说，初始化一个对象p。

<2> p.__proto__=Person.prototype;

<3> Person.call(p);也就是说构造p，也可以称之为初始化p。

 `1` `
 `2` `var` `Person = ``function` `() { };`
 `3` `var` `p = ``new` `Person();`
 `4` `alert(p.__proto__ === Person.prototype);`
 `5` ``

 `1` `
 `2` `var` `Person = ``function` `() { };`
 `3` ` Person.prototype.Say = ``function` `() {`
 `4` ` alert(``"Person say"``);`
 `5` `}`
 `6` `var` `p = ``new` `Person();`
 `7` `p.Say();`
 `8` ``

 `01` `
 `02` `var` `Person = ``function` `() { };`
 `03` ` Person.prototype.Say = ``function` `() {`
 `04` ` alert(``"Person say"``);`
 `05` `}`

 `06` `Person.prototype.Salary = 50000;`

 `07` `var` `Programmer = ``function` `() { };`
 `08` `Programmer.prototype = ``new` `Person();`
 `09` `Programmer.prototype.WriteCode = ``function` `() {`
 `10` ` alert(``"programmer writes code"``);`

 `11` `};`

 `12` `Programmer.prototype.Salary = 500;`

 `13` `var` `p = ``new` `Programmer();`
 `14` `p.Say();`
 `15` `p.WriteCode();`
 `16` `alert(p.Salary);`
 `17` ``

var p=new Programmer()可以得出p.__proto__=Programmer.prototype;

p1.__proto__=Person.prototype;

Programmer.prototype.__proto__=Person.prototype;
{"ft_lang_label":"__label__zh","ft_lang_prob":0.7030478,"math_prob":0.822735,"size":2032,"snap":"2020-24-2020-29","text_gpt3_token_len":1016,"char_repetition_ratio":0.25443786,"word_repetition_ratio":0.2244898,"special_character_ratio":0.26968503,"punctuation_ratio":0.21885522,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9770861,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,9,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-10T22:38:40Z\",\"WARC-Record-ID\":\"<urn:uuid:d96c254e-87a2-415a-861b-b5e181dd8cf5>\",\"Content-Length\":\"126773\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5d452cd3-16c5-4911-8397-9cdc0b425aa5>\",\"WARC-Concurrent-To\":\"<urn:uuid:fe936206-acb5-4542-b42e-c70a46b7a422>\",\"WARC-IP-Address\":\"121.42.88.23\",\"WARC-Target-URI\":\"http://www.sk616.com/thread-130.htm\",\"WARC-Payload-Digest\":\"sha1:L4FOZZ4SK6XK5MXVUQIJL7PCFHPNROVQ\",\"WARC-Block-Digest\":\"sha1:EPBHNS7M4O324SMMLB35YPGO5MYWLOEH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655912255.54_warc_CC-MAIN-20200710210528-20200711000528-00215.warc.gz\"}"}