用什么来代替Interlocked.Equals(What to use instead of Interlocked.Equals)
我有一些使用Interlocked.Equals比较值的遗留代码。 这些值可能是两个布尔值,或者它可能会将一个结构数组与空值进行比较。 Resharper抱怨Interlocked.Equals说“通过派生类型访问类型的静态成员”。 我知道Equals不是Interlocked类的成员,而是对象类的成员。 比较发生在一个线程中,所以我假设原始编码器想要做比较,作为原子操作,因此使用了互锁。 既然object.Equals不是原子,那么正确的线程安全的方式来做这些比较? 请注意,大部分数据是静态的,其中一些数据是静态易失性的。
I have some legacy code that uses Interlocked.Equals to compare values. The values may be two bools or it may compare an array of structs to null. Resharper complains about Interlocked.Equals saying "access to a static member of a type via a derived type". I know that Equals is not a member of the Interlocked class, but rather is a member of the object class. The comparison is happening in a thread so I assume the original coder wanted to do the comparison as an atomic operation hence the use of Interlocked. Since object.Equals is not atomic what is the proper, thread safe, way to do these kinds of comparisons? Note, most of the data is static, some of it is static volatile.
原文:https://stackoverflow.com/questions/3290945
最满意答案
构造对象时,构造函数的
prototype
属性被复制到新对象的__proto__
,因此当a
和b
保留旧原型(也包含.constructor
属性)时,c
使用新的空白原型(没有constructor
作为自己的财产)。 这是一个图表:
a = new A; b = new A
a = new A; b = new A
:在
A.prototype = {}; c = new A()
A.prototype = {}; c = new A()
正如您所看到的,直接分配到原型会破坏JS对象系统并可能导致令人惊讶的结果(您的问题就是这样)。 这就是为什么它一般不赞成。
When you construct an object, the constructor's
prototype
property is copied to the new object's__proto__
, so whilea
andb
retain the old prototype (which also contains the.constructor
property),c
uses the new blank prototype (which doesn't haveconstructor
as own property). Here's a diagram:After
a = new A; b = new A
:After
A.prototype = {}; c = new A()
As you can see, direct assignment to a prototype breaks the JS object system and may lead to surprising results (which your very question demonstrates). That's why it's generally frowned upon.
相关问答
更多-
为什么不创造新的功能Car function Vehicle(category) { this.category = category; return { Car: this.Car }; }; function Car(type) { this.wheels = []; this.type = type; return { type: this.type, wheels: this.wheels, ...
-
对于您给出的示例,您应该使用原型方法。 一般来说,这取决于。 第一种方法(构造函数中的初始化方法)的主要优点是,您可以通过使用方法中的构造函数中定义的局部变量来利用闭包。 这些变量在构造函数之外是不可直接访问的,所以有效地“私有”,这意味着你的API比这些变量被定义为对象的属性更清洁。 一些一般的经验法则: 如果您的方法不使用您的构造函数中定义的局部变量(您的示例不使用),则使用原型方法。 如果您正在创建大量的Dog ,请使用原型方法。 这样,所有“实例”(即由Dog构造函数创建的对象)将共享一组函数,而构 ...
-
构造函数字段没什么特别的,就像任何其他字段一样。 回答#1 : F.prototype = "string"; console.log(F.prototype.constructor); // function String logs function String因为它与 console.log("string".constructor); // function String 回答#2 : 内部__proto__字段的每个对象必须是typeof x === "object" 。 在原型链的末尾总是有 ...
-
function editor(){ /*...*/ } Object.assign(editor, { document(){//this is a new shortform for methods ;) //whatever } //... }); 问题是无论何时将对象文字分配给编辑器 ,您都将覆盖该函数: editor = {...}; 由于无法构造具有属性的函数,我们需要先创建一个函数,然后将属性分配给它(就像我上面所做的那样)。 function editor(){ / ...
-
无法真正理解Javascript中的构造函数和原型关系(Can't really understand constructor and prototype relationship in Javascript)[2022-01-20]
为什么这会打印“汽车”对象? 不是构造函数不是原型对象吗? 原型对象为什么不打印? 这就是Chrome(或您使用的浏览器)命名对象的方式。 如果仔细查看属性,它实际上是Car.prototype : 我正在尝试将构造函数中的prototype属性更改为“Vehicle”对象是做得对吗? 您无法更改现有对象的原型,只能扩展它。 设置Car.prototype = Vehicle; 将只更改Car 未来实例的原型,现有的仍然会引用原始的原型对象,它没有getName属性: // create a new ins ... -
更改原型时,Javascript对象丢失了构造函数(Javascript object lost constructor when the prototype was changed)[2022-08-08]
构造对象时,构造函数的prototype属性被复制到新对象的__proto__ ,因此当a和b保留旧原型(也包含.constructor属性)时, c使用新的空白原型(没有constructor作为自己的财产)。 这是一个图表: a = new A; b = new A a = new A; b = new A : 在A.prototype = {}; c = new A() A.prototype = {}; c = new A() 正如您所看到的,直接分配到原型会破坏JS对象系统并可能导致令人惊讶的结果 ... -
所有函数的标准是具有.prototype属性,该属性指向具有指向函数的.prototype属性的对象。 13.2创建Function对象 1)创建一个新的本机ECMAScript对象,让F成为该对象。 ... 16)让proto成为创建一个新对象的结果,这个对象将由表达式new Object()构造,其中Object是具有该名称的标准内置构造函数。 17)使用参数"constructor"调用proto的[[DefineOwnProperty]]内部方法,属性描述符{[[Value]]: F, { [[Wr ...
-
要在current对象上调用函数,不应该使用prototype ,只需调用它( this.say() )。 var Person = (function() { function Person() { this.say(); } Person.prototype.say = function() { alert("hello"); } return Person; })(); var person = new Person(); 要在 ...
-
当您键入rufus.constructor ,JavaScript会检查rufus对象上是否存在此类属性。 它不在那里,因为你没有在那里创造它。 然后它向上通过原型链并在rufus.__proto__.__proto__上找到这个属性,它指向Pet函数并输出此函数。 这里有更详细的解释。 声明函数时: function Pet() { /* ..code.. */ } 解释器从您的声明中创建新的函数对象。 与函数一起,创建并填充它的prototype属性。 这个默认值是prototype是一个带有属性con ...
-
您可以将原型函数和值视为所有实例的默认值。 为什么你看到TypeError是因为你试图调用dany的原型和方法test() 。 请尝试使用dany.test() 。 在阅读它的工作原理时, 这将是您最好的选择 。 TLDR; 您正在获取TypeError因为user函数的实例没有自己的原型。 但是,您可以通过__proto__快捷方式访问实例原型。 You can think of prototype functions and values as default values for all instan ...