简体   繁体   English

Object.defineProperty在构造函数中不执行任何操作

[英]Object.defineProperty does nothing in constructor

I've recently restructured my three.js project and I started to encounter a problem where every object seems to be rendered with the exact same geometry and material. 我最近重组了three.js项目,并开始遇到一个问题,其中似乎每个对象都使用完全相同的几何形状和材质进行渲染。 I tracked the problem to this constructor in the debugger: 我在调试器中将此问题跟踪到此构造函数:

function Geometry() {
    Object.defineProperty( this, 'id', { value: GeometryIdCount() } );
    this.uuid = exports.Math.generateUUID();
    ...

Chrome shows no properties on the this object after the Object.defineProperty , and id comes back as undefined when things try to use it later. Chrome在Object.defineProperty之后在this对象上不显示任何属性,并且当以后尝试使用它时, id返回为undefined However, when I debug older projects I've made using three.js, the id is defined on this in the debugger after the call. 然而,当我调试,我使用three.js所取得旧项目,该id是在定义this呼叫后调试器。

What could cause Object.defineProperty to do nothing? 是什么导致Object.defineProperty不执行任何操作? The most likely culprit is that I've switched from using node.js require calls from Javascript to include three to ES6 import calls from Typescript with a TS compiler, but it's not clear to me why that would have this effect on this constructor. 造成这种情况的最可能原因是我不再使用node.js,而require使用Java调用通过TS编译器从Typescript包括三个调用到ES6 import调用,但是我不清楚为什么会对构造函数产生这种影响。

Just saying that " id comes back as undefined" doesn't necessarily mean that Object.defineProperty did nothing, although I do understand that you looked at the properties of this directly after the Object.defineProperty call, and I can't explain that. 只是说“ id返回为未定义状态”并不一定意味着Object.defineProperty什么也不做,尽管我确实知道您在Object.defineProperty调用之后直接查看了它的属性, this我无法解释。 If GeometryIdCount() returns undefined, then that would explain why the value is undefined later. 如果GeometryIdCount()返回undefined,那么这将解释为什么以后undefined该值的原因。 Have you verified that the property itself is undefined, rather than the property value, after the constructor completes? 构造函数完成后,您是否已验证属性本身是未定义的,而不是属性值?

It may be like using the word "utilize" instead of "use", or maybe Typescript, ES6 or three.js require it, but why don't you instead write: 可能就像使用单词“ utilize”而不是“ use”,或者可能是Typescript,ES6或three.js都需要它,但是为什么不写:

this.id = GeometryIdCount();

This will still assign unknown to this.id if the function returns undefined, however. 但是,如果函数返回未定义,这仍将为this.id分配未知数。

Update: Your question got me to research exactly what Object.defineProperty does , and it was surprising and very informative. 更新:您的问题使我能够精确地研究Object.defineProperty的功能 ,这令人惊讶并且非常有用。 When I worked with WebGL, I didn't use three.js, so I incorrectly assumed that Geometry was a function that you wrote. 当我使用WebGL时,我没有使用three.js,因此我错误地认为Geometry是您编写的函数。

When you call Object.defineProperty in the way that you describe, (setting the 3rd argument to an Object with only the "value" property) it sets the writable, configurable and enumerable properties, which all Object properties have, to false. 当您以描述的方式调用Object.defineProperty时(将第三个参数设置为仅具有“值”属性的对象),它将把所有对象属性都具有的可写,可配置和可枚举属性设置为false。 The statement this.id = GeometryIdCount() , on the other hand, will set those 3 properties to true. 另一方面,语句this.id = GeometryIdCount()会将这三个属性设置为true。 The author of three.js apparently wanted to assign a unique key to each Geometry object and make it so the user can't change its value. three.js的作者显然想为每个Geometry对象分配一个唯一的键并使其唯一,以便用户无法更改其值。 I didn't look for other reasons. 我没有其他原因。

I recommend that you make a copy of your project and whittle out anything that you want to keep secret, (if you want to post it here) and get rid of as much distracting code as you can, to make it easier to determine what is causing the problem. 我建议您复制项目,并减少任何您想保密的内容(如果要在此处发布),并尽可能多地分散代码,以便更轻松地确定什么是造成问题。 You know, stop removing code when you can no longer reproduce this bad behavior where the id property is not created or it is undefined. 您知道,当您不再能够重现未创建id属性或undefined的不良行为时,请停止删除代码。 I made a small program and tried to guess how to repro the problem, doing bad things like Geometry.prototype.id = undefined before calling var g = Geometry(...) , (without the new keyword) but was unable to reproduce the problem. 我制作了一个小程序,尝试猜测如何解决该问题,在调用var g = Geometry(...)之前做了一些类似Geometry.prototype.id = undefined坏事情,(没有new关键字),但是无法重现问题。 I didn't go so far as to try to override the behavior of GeometryIdCount , or prevent the creation of the id property. 我并没有尝试覆盖GeometryIdCount的行为,或者阻止了id属性的创建。 That seems like intentional sabotage, and I doubt you suspect that in your case. 这似乎是故意破坏,我怀疑您是否怀疑您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM