cancel
Showing results for 
Search instead for 
Did you mean: 

Server-side javascript : equals() or == ?

dgenard
Champ on-the-rise
Champ on-the-rise
Hi,

how does the Alfresco javascript engine computes equality ?

In regular javascript, the == operator returns true if 2 string values have the same content.
In Java, we know that the meaning isn't the same. We use equals() method to compare the content of 2 strings.

After some investigations, I did notice that Alfresco javascript engine doesn't follow the javascript meaning of == for node properties.
In example, this might not be true
document.id == doc.id
even if document and doc represent the same node.
In this case,
document.id.equals(doc.id)
returns true.

I don't know if this is a bug or feature…
This applies to Alfresco 2.1.0E.
What is the valid way to test equality of 2 node properties in Alfresco server-side javascript ?

Denis
3 REPLIES 3

kevinr
Star Contributor
Star Contributor
The problem is that some of the strings you have are actually Java java.lang.String objects and some are javascript 'string' objects. If you do this:

var id = new String(document.id);
if (someid == id)
{…}
then it should work.

Kevin

louise
Champ in-the-making
Champ in-the-making
Same problem here, but the solution (.toString() isn't helps):


if(String(first_node.nodeRef) === String(second_node.nodeRef)) {
  … same nodeRef
} else {
  … different
}

sbuckle
Champ in-the-making
Champ in-the-making
I take it the two node references you are comparing ARE the same? I can't see anything wrong with the example JavaScript you gave. Based upon your example I came up with a quick test:


// your noderef here
var nodeRef = "workspace://SpacesStore/4658fedc-f431-45fr-b834-c7a7441zs624";

var x = search.findNode(nodeRef);
var y = search.findNode(nodeRef);

if (String(x.nodeRef) === String(y.nodeRef)) {
  logger.log("Equal");
} else {
  logger.log("Not equal");
}

As expected, it prints out "Equal" in the log.