Why don't the comparison results match?
This is a QML function. Unclear why the comparison results are different?
...
function someFunction() {
var id1 = AAAAA.objectId // it is QByteArray property of C++ object
var id2 = BBBBB.objectId // it is QByteArray property of C++ object
var isEqual = id1 === id2
var isEqualToString = id1.toString() === id2.toString()
console.log("=============================")
console.log("id1: ", id1)
console.log("id2: ", id2)
console.log("isEqual: ", isEqual)
console.log("id1.toString(): ", id1.toString())
console.log("id2.toString(): ", id2.toString())
console.log("isEqualToString: ", isEqualToString)
}
...
console result:
id1: 2607d35d-d9d0-49af-b944-3fc60993890a
id2: 2607d35d-d9d0-49af-b944-3fc60993890a
isEqual: false
id1.toString(): 2607d35d-d9d0-49af-b944-3fc60993890a
id2.toString(): 2607d35d-d9d0-49af-b944-3fc60993890a
isEqualToString: true
+3
source to share
1 answer
id1
and id2
are javascript object
:
console.log("typeof id1: ", typeof id1)
console.log("typeof id2: ", typeof id2)
outputs:
qml: typeof id1: object
qml: typeof id2: object
In javascript, two type variables object
are equal if and only if they represent the same object. (In C ++ this would be like comparing pointers, they are equal if they point to the same address).
Knowing this, we can take another look at your code:
var id1 = AAAAA.objectId // Creates an object and reference it by id1
var id2 = BBBBB.objectId // Creates of another object and reference it by id2
var isEqual = id1 === id2 // false because id1 and id2 reference 2 different objects, regardless of their content
var isEqualToString = id1.toString() === id2.toString() // true because you compare strings and strings are compared using their content
+3
source to share