> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/12javascript-quan-gong-lve-ff1a-ke-fu-js-de-qi-guai-bu-fen/124javascript-de-wu-jian-dao-xiang-yu-yuan-xing-ji-cheng/1244reflection-yu-extend.md).

# 1.2.4.4.Reflection 與 Extend

* 反射 (reflection)
  * 一個物件可以看到自己的東西, 然後改變自己的屬性跟方法

```
    var person = {
        firstname: 'Default',
        lastname: 'Default',
        getFullName: function(){
            return this.firstname + ' ' + this.lastname;
        }
    }

    var john = {
        firstname: 'John',
        lastname: 'Doe'
    }

    //don't do this
    john.__proto__ = person;

    //將會遍歷這個物件的所有屬性跟方法
    //印出:
    //firstname: 'Default',
    //lastname: 'Default',
    // getFullName: function(){
    //       return this.firstname + ' ' + this.lastname;
    // }
    for (var prop in John) {
        console.log(prop + ': ' + john[prop]);
    }

    //確認prop是否是John所擁有的
    for (var prop in John) {
        if (John.hasOwnProperty(prop)) {
            console.log(prop + ': ' + john[prop]);
        }
    }

    var jane = {
        address: '111 Main St',
        getFormalFullName: function(){
            return this.lastname + ' ' + this.firstname;
        }
    }
    var jim = {
        getFirstName: function(){
            return this.firstname;
        }
    }

    //把所有的屬性與方法放到john
    _.extend(john, jane, jim)
```
