使用Object.defineProperties定义属性

时间:2015-09-07 18:57:52

标签: javascript oop prototype

请帮助理解为什么行obj['prop'] = 'zzz';不起作用,甚至在"use strict";开启时显示错误,并且在使用Object.defineProperty(obj, 'prop', {value:'zzz'});时工作正常? (虽然delete工作得很好)

       //"use strict";

        var obj = Object.defineProperties({}, {
            prop: {
                value: 'aaa',
                writeable: true,
                enumerable: true,
                configurable: true
            }
        });

        console.log(obj.prop);
        obj['prop'] = 'zzz';
        //Object.defineProperty(obj, 'prop', {value:'zzz'});
        console.log(obj.prop);
        //delete obj.prop;
        //console.log(obj.prop);

1 个答案:

答案 0 :(得分:2)