尝试使用访问器定义属性时出错。这是我的代码
var person = {}; Object.defineProperty(person, 'birthYear', { value: 1997, writable: true, enumerable: true, configurable: false, set: function (value) { this.birthYear = value; }, get: function() { return 'Birth year - ' + this.birthYear; } }); console.log(person.birthYear);
错误文字:
无效的属性描述符。不能同时指定访问者和值 或可写属性。
如果我不能定义set和get这样的方法我怎么做呢?
答案 0 :(得分:8)
查看错误消息:
不能同时指定访问者和值或可写属性。
当您明确说明尝试写入时会发生什么时,说明属性是否可写是没有意义的。
删除:
from collections import Counter
counter = Counter(Word_array)
the_count_of_some_word = counter["some_word"]
#printing the counts
for word, count in counter.items():
print("{} appears {} times.".format(word, count)
当您在阅读时动态计算该值时,您无法为其提供值,因此请删除:
writable: true,
然后,您会收到有关递归的错误,因为每次尝试阅读 value: 1997,
时,您的getter函数都会尝试阅读person.birthYear
。
所以将值存储在其他地方。
person.birthYear