Re: [whatwg/url] Make property accessors into owned properties, or at least enumerable (#301)

> I don't understand "normal object models" as defined in OP. Surely the "normal" one is the one defined by JavaScript, which puts things on the prototype for all its objects?

I would say that JavaScript's basic object model is to store data in owned properties, and only put functions ("methods") on its prototypes.  In a sense, `URL` complies with that concept because all of its public properties are essentially functions in that they are getters and setters but from an outside perspective they would likely be considered simple data properties if it weren't for their computed nature [if one of them were to be updated].

> What are you trying to accomplish that works with say Map, but not URL?

Yes, I guess `Map` is similar.


Again, the main frustration is that they are not possible to compare on a property level with typical object comparison functions without adding custom code specific to handling `URL` objects, or without also iterating through all potentially inherited properties (i.e. with `for...in`).

```js
const URL_KEYS = Object.keys( URL.prototype );

function urlToPlainObject( url ) {
  var urlObj = url ? {} : null;
  URL_KEYS.forEach(function( URL_KEY ) {
    if ( typeof url[ URL_KEY ] === 'string' ) {
      urlObj[ URL_KEY ] = url[ URL_KEY ];
    }
  });
  return urlObj;
}


var url = new URL('http://example.com/path/to/somewhere?curious=yes#sir');

console.log( JSON.stringify( url ) );
// => {}

console.log( JSON.stringify( urlToPlainObject( url ) ) );
// => {"href":"http://example.com/path/to/somewhere?curious=yes#sir","origin":"http://example.com","protocol":"http:","username":"","password":"","host":"example.com","hostname":"example.com","port":"","pathname":"/path/to/somewhere","search":"?curious=yes","hash":"#sir"}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/301#issuecomment-297993562

Received on Friday, 28 April 2017 13:11:34 UTC