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


Groups > comp.lang.javascript > #12791

Re: Adding new method to existing object

Message-ID <7404478.9nW1t498yI@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-03-16 15:31 +0100
Subject Re: Adding new method to existing object
Newsgroups comp.lang.javascript
References <31786995.22.1331895413978.JavaMail.geo-discussion-forums@vbat19> <571e5767-fc18-4945-b356-4004af1145e8@q12g2000yqg.googlegroups.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


David Mark wrote:

> On Mar 16, 6:56 am, cezary.tomc...@gmail.com wrote:
>> I want to extend my prototype object using another object. Just created
>> some example code:
>>
>> http://jsfiddle.net/u9e7R/13/
>>
>> I tested the addMethod function in Firefox 10,11, Chrome 17 and IE 9. But
>> it will fail in IE8. In IE8 console when I run jsCode().test2(); I see
>> 'Object does not support property or method "test2"'.
> 
> You added it too late to have any effect on the constructed object.
> Augmenting the prototype property of the constructor won't do anything
> until you construct another object.

Although the explanation might hold some truth for this situation (I had no 
time to check yet), in itself it is utter nonsense.  A property is resolved 
against the prototype chain only on property access.  Therefore, if you 
augment the prototype of the constructor of an object, that affects all 
objects created with that constructor (no matter the current value of the 
`constructor' property), no matter when they have been constructed.  It is 
why augmenting `Object.prototype', no matter where in the program, can be 
that harmful to program flow if appropriate precautions have not been taken.

There is no difference between ECMAScript implementations there.

  function Constructor() {}

  var obj = new Constructor();

  Constructor.prototype.answer = 42;

  /* 42 */
  console.log(obj.answer);


PointedEars
-- 
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