Расширить объект в Javascript

    // sort algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::sort
    #include <vector>       // std::vector
    using namespace std;
    int main () {
        char myints[] = {'F','C','E','G','A','H','B','D'};
        vector<char> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
        // using default comparison (operator <):
        sort (myvector.begin(), myvector.end());           //(12 32 45 71)26 80 53 33
        // print out content:
        cout << "myvector contains:";
        for (int i=0; i!=8; i++)
            cout << ' ' <<myvector[i];
        cout << '\n';
        system("PAUSE");
    return 0;
    }
0
задан laoqiren 13 July 2018 в 09:25
поделиться

1 ответ

Object.assign копирует значения свойств. Вам нужно «переопределить» свойства, используя Object.defineProperties(obj, Object.getOwnPropertyDescriptors(extend)).

Вот почему вы получаете ошибку при попытке получить значение csrfOptions.

const defaultOptions = {};

const extend = {
   get csrfOptions() {
        return Object.assign({}, defaultOptions, this.securityOptions.csrf); //can't read property csrf of undefined
    }
}

const obj = {
   securityOptions: {
     csrf: {
       name: 'laoqiren'
     }
   }
}

Object.defineProperties(obj, Object.getOwnPropertyDescriptors(extend))

console.log(obj.csrfOptions);

0
ответ дан Yury Tarabanko 17 August 2018 в 13:17
поделиться
Другие вопросы по тегам:

Похожие вопросы: