- From: Austin William Wright <aaa@bzfx.net>
- Date: Tue, 5 Nov 2013 20:54:07 -0700
- To: Matteo Collina <matteo.collina@gmail.com>
- Cc: Ruben Verborgh <ruben.verborgh@ugent.be>, "public-rdfjs@w3.org" <public-rdfjs@w3.org>
- Message-ID: <CANkuk-WxjYR27HwXjU-k5Kg2pCcL7jBQfTucYjfov9352hw_1A@mail.gmail.com>
I don't modify any builtins or other libraries unless explicitly desired, nor does any part of the library depend on the functionality. It's intended to be turned on by applications and entire ECMAScript platforms who define "the" global ECMAScript context, instead of libraries. Yeah, it's definitely not a good idea to populate stuff at the global scope. Though it's mostly an issue for things that provide some sort of state, instead of just a library. For example, rdf.environment, by default, is an instance of rdf.RDFEnvironment. Please don't copy this example of providing a default instance. It's just a bad idea, it gives people bad ideas that they can safely use it. There's better ideas: CURIE prefixes should be declared as variables at the top level of the file, not inside strings, then they're scoped to the code file and not the instance that was passed around. For example, I provide `rdf.rdfns('type')` to produce rdf:type, there's also rdfs and xsd builtin in the same fashion. Nonetheless, I don't take issue with expanding builtin prototypes and using them in an application. I wouldn't describe the functionality as monkey-patching, which would imply replacing or wrapping around existing function definitions. The functionality I describe just extends the prototype of native objects. The prototypical nature of ECMAScript means it's mostly syntactical (i.e. (5).nodeType() versus nodeType.call(5) or nodeType(5) -- the function declaration and logic is the same in either case). Austin. On Tue, Nov 5, 2013 at 11:58 AM, Matteo Collina <matteo.collina@gmail.com>wrote: > > > > 2013/11/4 Austin William Wright <aaa@bzfx.net> > >> >> Yet this is what I do with 'rdf'. Try it yourself: >> >> > require('rdf').setBuiltins(); >> > var a = 4; >> > a.datatype >> 'http://www.w3.org/2001/XMLSchema#integer' >> > a.nodeType() >> 'TypedLiteral' >> > (4.5).datatype >> 'http://www.w3.org/2001/XMLSchema#decimal' >> > "_:x".nodeType() >> 'BlankNode' >> >> Magic! >> >> >>> > This is far more powerful than merely encoding all literals as a >>> string. >>> >>> If it were possible, yes. Ruby offers this; I think it’s really cool. >>> >> >> More magic, brought to you by the magic `valueOf` method: >> >> Number.prototype.tl = function(t) { >> Object.defineProperty(this,'datatype', { writable: false, configurable >> : false, enumerable: false, value: t } ); >> return this; >> } >> > var x = (5).dt('http://www.w3.org/2001/XMLSchema#int'); >> > x+2 >> 7 >> > x.datatype >> 'http://www.w3.org/2001/XMLSchema#int' >> >> ... kind of. >> >> > typeof x >> 'object' >> > ++x >> 6 >> > typeof x >> 'number' >> > > I think that monkey-patching Javascript core object is really really bad, > due to all the failures of previous libraries that have tried. > In fact, the Prototypejs (http://prototypejs.org/) library did that. > Prototypejs have lost the 'war' against jQuery. > The main issue is that we have only one JS scope. If we all monkey-patch > core object, we end up with a big mess and everything stop working. > > So, don't do that. JS developers won't use your library, because they have > already been there. > We may disagree about that ;) > > Cheers, > > Matteo > >
Received on Wednesday, 6 November 2013 03:54:35 UTC