Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12837

Re: Adding new method to existing object

Message-ID <2919534.8F1xFLJzEo@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-03-17 02:04 +0100
Subject Re: Adding new method to existing object
Newsgroups comp.lang.javascript
References (1 earlier) <571e5767-fc18-4945-b356-4004af1145e8@q12g2000yqg.googlegroups.com> <7404478.9nW1t498yI@PointedEars.de> <ee3d3a2a-9711-425a-a151-cba30d66b41f@g16g2000yqe.googlegroups.com> <1795217.bUTGlsRvRN@PointedEars.de> <d4454218-f394-4bba-8b0f-6e1e17ea2281@2g2000yqk.googlegroups.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


David Mark wrote:

> On Mar 16, 7:10 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> wrote:
>> David Mark wrote:
>> > Thomas 'PointedEars' Lahn wrote:
>> >> function Constructor() {}
>> >> 
>> >> var obj = new Constructor();
>> >> 
>> >> Constructor.prototype.answer = 42;
>> >> 
>> >> /* 42 */
>> >> console.log(obj.answer);
>> > 
>> > Yes, I see that works; so clearly I was wrong. Regardless, that's an
>> > execrable pattern and certainly not one that I would use. Any project
>> > that did something like that would be re-factored long before I ran it
>> > for the first time. :)
>>
>> It is a potentially useful pattern if you consider that primitive values
>> are internally converted to corresponding objects if they are used in a
>> /MemberExpression/.  For example, I can see no harm in writing
>>
>> String.prototype.htmlEscape = function () {
>> return this.replace(/[\u0080-\uFFFF]/g, function (m) {
>> return "&#" + m.charCodeAt(0) + ";";
>> });
>> };
>>
>> console.log("|@#°½¬¦¢øĸjħŋđδ".htmlEscape());
>>
>> /* optional */
>> delete String.prototype.htmlEscape;
>>
>> instead of
>>
>> var htmlEscape = function (s) {
>> return s.replace(/[\u0080-\uFFFF]/g, function (m) {
>> return "&#" + m.charCodeAt(0) + ";";
>> });
>> };
>>
>> console.log(htmlEscape("|@#°½¬¦¢øĸjħŋđδ"));
> 
> I meant the bit where the prototype is augmented after constructing
> objects (not to mention the way it was done in this case!) :(

`String' refers to a `Function' instance.  "…" is converted to a `String' 
instance (as if by `new String("…")' in a /MemberExpression/.  I do not see 
your point, and I can see no reason for dismay.

I also see no reason for a full quote.  That you are removing quote marks 
(`>') from "empty" lines on top of that is disturbing, as it makes reading 
quoted quotes considerably harder (the reason for quote marks: one mark for 
each *level*, *no matter* if the original line was empty).

> Yes, I use a variations of the latter a lot, though not quite as
> compartmentalized as that. Richard once had a page that described
> several different variations on this theme. If that's where you got
> this and the page is still up, please send me a link as it was a
> useful resource.

I was posting from memory.  I do not know who had that idea.

-- 
When all you know is jQuery, every problem looks $(olvable).

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-16 03:56 -0700
  Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 04:50 -0700
    Re: Adding new method to existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-16 15:31 +0100
      Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 11:51 -0700
        Re: Adding new method to existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-17 00:10 +0100
          Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 17:25 -0700
            Re: Adding new method to existing object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-17 02:04 +0100
              Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 18:15 -0700
    Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-16 10:18 -0700
      Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 11:56 -0700
      Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-16 13:01 -0700
        Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-17 04:58 -0700
          Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 16:04 -0700
            Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-18 04:24 -0700
              Re: Adding new method to existing object John G Harris <john@nospam.demon.co.uk> - 2012-03-18 13:16 +0000
  Re: Adding new method to existing object John G Harris <john@nospam.demon.co.uk> - 2012-03-16 17:09 +0000
    Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-16 10:46 -0700
  Re: Adding new method to existing object Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-16 13:43 -0700
  Re: Adding new method to existing object Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-16 20:15 -0700
    Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-17 05:17 -0700
    Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-17 05:24 -0700
      Re: Adding new method to existing object David Mark <dmark.cinsoft@gmail.com> - 2012-03-17 16:16 -0700
        Re: Adding new method to existing object cezary.tomczyk@gmail.com - 2012-03-18 04:27 -0700

csiph-web