Allen Wirfs-Brock (2013-11-01T00:50:35.000Z)
domenic at domenicdenicola.com (2013-11-12T18:49:43.367Z)
On Oct 31, 2013, at 2:56 PM, Andrea Giammarchi wrote: > https://github.com/WebReflection/circular-json/blob/master/src/circular-json.js#L108 > > Just one case, still `instanceof` is used a lot and usually for good reasons, ... Well I promised I'd explain how I'd eliminate use of `instanceof` so here goes. Note that since this is in the context of talking about possible ES extensions I'll assume those are available. First, you're using (lines 20) `String` wrapper objects(with the name `String` aliased to `$String`) to pass around string values that require special processing. Personally, I find this a little too subtle. In ES6 I'd do this way instead: ```js const isSpecialString = Symbol.for("isSpecialString"); //registration proposal in http://esdiscuss.org/topic/comments-on-sept-meeting-notes#content-76 class SpecialString extends String { constructor(str) { super(str.slice(1)) //move string preprocessing into constructor } [isSpecialString]() {return isSpecialString in this}; //not really intended to be called } ``` In line 110 I'd use ```js Array.isArray(current) ``` instead of ```js current instanceof Array ``` (note that JSON processing was one of the original use cases that motivated `Array.isArray`) In line 114, instead of ```js current instanceof $String ? ``` i'd use ```js isSpecialString in current ? //note works cross-realm, if that is what is desired. ``` I may be missing something WRT possible cross-realm issues, but in line 126 I'd replace ```js current instanceof Object ``` with ```js typeof current == 'object' ``` but, I'm really thinking that I must be missing something that motivated your use of instanceof. Is the supposed to be a Realm check? > ... not because we are all bad developers (I hope ...) No, but possibly developers who learned (or were taught be people who learned) about such things in a language context where static nominal class tests make more sense.