问题标签 [defineproperty]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
2742 浏览

javascript - Object.defineProperty() vs Object.prototype.property vs Object.property 什么时候用什么?

有人可以给我一个很好的用例,说明何时使用 Object.defineProperty()、Object.prototype.property 和 Object.property。

0 投票
1 回答
173 浏览

javascript - Object.defineProperties() 中的自引用

当向一个对象添加多个属性时,添加它们Object.defineProperties()通常会产生比一个一个或一组添加更清晰、更易读的代码。

但是,如果一个属性引用另一个属性,Object.defineProperties()则无法处理它。考虑一个虚拟示例:

现在很明显,第二种解决方案总是有效的。但是,有人会假设第一个也可以工作,因为它似乎是另一个的简写,并且无论如何都建议对属性进行顺序处理。

我没有找到关于此的明确文档。

所以问题是:

  • 这是有意的限制,还是背景中的技术复杂性?
  • 它是否记录在某处?
  • 它是实现特定的还是标准的行为?

编辑:回答清楚,并在那里查看我的评论。这是一个简单的对象字面量问题(不是问题,而是 :),与 Object.defineProperties() 没有直接关系。

0 投票
2 回答
206 浏览

javascript - 如何使用 js es5 Object.defineProperty 重载 = 运算符

我使用什么来重载=JS 对象的运算符Object.defineProperty

这是定义简单属性的标准 es5 语法。
Nowobj.var是具有重载=运算符的属性。
但我实际上要做的是=为自身重载运算符obj

如何=为对象本身重载运算符?

有可能(在 es5 中)吗?

0 投票
1 回答
109 浏览

javascript - 在 elem.dataset 上使用 Object.defineProperty,但属性仍然可以更改

我正在尝试使用 Object.defineProperty 在画布元素的数据集上设置不可更改的属性。在普通物体上,这可以正常工作。但是当我尝试在数据集对象上执行此操作时,该属性仍然是可变的。

使用 writable: false 也不能解决它。有没有办法解决这个问题,它是一个错误,我不应该以这种方式使用 Object.defineProperty 吗?

0 投票
1 回答
2066 浏览

javascript - 打字稿错误引用 _this

我试图String.PrototypeTypeScript中定义一些属性:

javaScript原型中,this指的是调用方法的对象(在这种情况下是字符串值)。但是文件的编译输出是:

TypeScript 编译器添加变量_this并引用它。

这是一个错误还是我的实现有问题?

0 投票
1 回答
228 浏览

javascript - JS:如何定义数值变量的属性

我几乎没有类型的变量number(或者也有字符串)。我想为这些变量添加一些元数据。

当我尝试使用Object.defineProperty(myvar, "prop", {/*getter & setter*/})它时,它会给出一个错误,即 myvar 不是对象。

如何使用 getter 和 setter 方法在非对象上定义属性?

我不想添加任何东西,Number.prototype因为我只希望在少数变量上使用这些属性。

0 投票
2 回答
73 浏览

javascript - how to use 'set' on object properties when the property type has a 'set' of it's own?

If '_position' is a Point object member of Sprite, with Number members x and y, and its own get/set definitions, how can I apply a higher level 'set' so this will work (EDIT: I would like to reach the 'set' function in Sprite, rather than the 'set' function in Point):

What happens currently is that "this.position" goes to the "position.get" property, and then "this.position.set" goes through to the Point set function. It seems like attempts to 'set' any object with it's own get/set will drop down to the lowest level.

It feels like there must be a way to change this behaviour, but my google and stackoverflow searches are not coming up with an answer. It's likely there's a proper term for what I want to do here, without which I'm struggling the phrase the question correctly.

For instance: this is close, but doesn't quite work in my situation

EDIT: I don't control the source file for Point, or the function where the this.position.set(x,y); is held. Although I may be able to get permission for a change to the calling syntax if I can prove that it won't break the other objects which actually want to use the Point set function.

0 投票
1 回答
1510 浏览

javascript - 使用带有 Angular 的 Object.defineProperty 会抛出 TypeError: Cannot assign to read only property (property) of #

I have an object on my scope, and I want that object to have a few non-enumerable properties, but after setting the enumerabl

I have an object on my scope, and I want that object to have a few non-enumerable properties, but after setting the enumerable descriptor to false, whenever I try to change the value, I get: "TypeError: Cannot assign to read only property (property) of #"

... even though I am specifically setting it's writeable and configurable descriptors to true. Why is this happening, and what can I do to fix it? In non-strict mode it doesn't throw an error, it just doesn't do anything. This should work fine according to: MDN ...

code:

if you run the above code snippet it throws: TypeError:


You have a typo. Your writeable key in the property descriptor should be writable.

0 投票
0 回答
231 浏览

javascript - 覆盖“scrollTop”属性并在getter中达到值

对于自定义滚动条脚本,我想覆盖默认的 scrolltop 属性。现在我遇到了问题,我需要先达到该值才能对其进行修改。我用

做这项工作。有没有人有想法或替代方案?

此致,

西蒙

0 投票
1 回答
702 浏览

javascript - Is it valid to use Object.defineProperties with symbols?

Let's take the following code:

Is this valid?

With the native Object.defineproperties code we get in the console.log true.

With the polyfill of zone.js

which is of the form of :

we get for the same code of console.log undefined.

This is because of the Object.keys function. I googled around and did not find in any place if this should be allowed or not.