Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e09ba8a

Browse files
committedMar 10, 2015
make the jsog_object_id a const
1 parent bc99489 commit e09ba8a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed
 

‎src/JSOG.coffee

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ nextId = 1
1010
# Older browser compatibility
1111
isArray = Array.isArray || (obj) -> Object.prototype.toString.call(obj) == '[object Array]'
1212

13+
JSOG_OBJECT_ID = '__jsogObjectId'
14+
1315
#
1416
# Take a JSON structure with cycles and turn it into a JSOG-encoded structure. Adds
1517
# @id to every object and replaces duplicate references with @refs.
1618
#
17-
# Note that this modifies the original objects adding __object_id fields and leaves
19+
# Note that this modifies the original objects adding __jsogObjectId fields and leaves
1820
# them there. There does not appear to be another way to define object identity in JS.
1921
#
2022
JSOG.encode = (original) ->
@@ -24,10 +26,10 @@ JSOG.encode = (original) ->
2426

2527
# Get (and if necessary, set) an object id. This ends up being left behind in the original object.
2628
idOf = (obj) ->
27-
if !obj.__jsogObjectId
28-
obj.__jsogObjectId = "#{nextId++}"
29+
if !obj[JSOG_OBJECT_ID]
30+
obj[JSOG_OBJECT_ID] = "#{nextId++}"
2931

30-
return obj.__jsogObjectId
32+
return obj[JSOG_OBJECT_ID]
3133

3234
doEncode = (original) ->
3335
encodeObject = (original) ->
@@ -37,7 +39,7 @@ JSOG.encode = (original) ->
3739

3840
result = sofar[id] = { '@id': id }
3941
for key, value of original
40-
if key != '__jsogObjectId'
42+
if key != JSOG_OBJECT_ID
4143
result[key] = doEncode(value)
4244

4345
return result

0 commit comments

Comments
 (0)
Failed to load comments.