Closed Bug 337465 Opened 19 years ago Closed 18 years ago

if a function prototype contains an object when creating a fct instance obj scope becomes invalid

Categories

(Core :: JavaScript Engine, defect)

1.8 Branch
x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: dan.rades, Unassigned)

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 When a function prototype contains an object(let's say aClass.prototype.aProp) any any instance of that function(let's say aObj) will not have the property aObj.aProp as an new,class-independent object but rather as an reference to aClass.prototype.aProp Example: aClass = function(){} aClass .prototype = { aProp : { text : 'some text'; } }; aObj1 = new aClass; aObj2 = new aClass; aObj2.aProp.text='lorem ipsum dolor'; alert(aObj1.aProp.text);// will alert 'lorem ipsum dolor' alert(aClass.prototype.aProp.text);// will alert 'lorem ipsum dolor' alert(aClass.prototype.privateProperties.text);//rezultat: seo sucks. i do spam in loc de seo is good alert(aClass.prototype.anotherProperty);//rezultat: spam is bad, asa e normal Reproducible: Always Steps to Reproduce: 1. Create a new HTML page 2. Place the code above into <script> </script> tags 3. Open it.
> alert(aClass.prototype.privateProperties.text);//rezultat: seo sucks. i do spam > in loc de seo is good > alert(aClass.prototype.anotherProperty);//rezultat: spam is bad, asa e normal As i cannot edit the description, please discard those last 2 line. I first reported this bug on a romainan seo forum and made copy/paste here
Reporter, do you still see this problem with the latest Firefox 2? If not, can you please close this bug as WORKSFORME. Thanks!
Whiteboard: CLOSEME 06/27
Version: unspecified → 1.5.0.x Branch
Steve, the problem persist with the latest Firefox version (2.0.0.4). As a sidenote, I've tested with IE6 and the same thing happens.
Severity: major → normal
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: 1.5.0.x Branch → 1.8 Branch
I presume this is filed against Javascript so moving accordingly. If it also happens in IE6 then it might not be a bug; but i'm not the chap to say one way or the other.
Whiteboard: CLOSEME 06/27
Your problem here is that the prototype given to instances created by new is shared by all of those instances. Therefore objects on that prototype are also shared, and if you change an object, it appears to change it for all instances created from that prototype. If you want to create non-shared objects, you need to do so in the constructor, which will create per-instance properties on all instances of the class: js> aClass = function () { this.aProp = { text: "hello, world" } } function () { this.aProp = {text: "hello, world"}; } js> obj1 = new aClass; obj2 = new aClass; [object Object] js> obj1.aProp.text = "ipsor, etc." ipsor, etc. js> obj2.aProp.text hello, world
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → INVALID
Blake, it's not quite true what you're saying. The prototype has to be exactly what its name says: a prototype. Is not a common object that instances of a class share but rather inherit. It would be almost useless otherwise. Please check the example below, it's a more explanatory than the first i posted aClass = function(){} aClass .prototype = { aProp : { text : 'some text' }, bProp : 4 }; aObj1 = new aClass(); aObj2 = new aClass(); aObj1.aProp.text='dummy text here'; aObj2.aProp.text='lorem ipsum dolor'; aObj1.bProp = 20; aObj2.bProp = 40; alert(aObj1.bProp);// will alert 20 - CORRECT alert(aObj2.bProp);// will alert 40 - CORRECT alert(aClass.prototype.bProp);// will alert 4 - CORRECT alert(aObj1.aProp.text);// will alert 'lorem ipsum dolor' - WRONG, should be 'dummy text here' alert(aObj2.aProp.text);// will alert 'lorem ipsum dolor' - CORRECT alert(aClass.prototype.aProp.text);// will alert 'lorem ipsum dolor' - WRONG, should alert 'some text'
Status: RESOLVED → VERIFIED
Status: VERIFIED → UNCONFIRMED
Resolution: INVALID → ---
(In reply to comment #6) > aObj1.aProp.text='dummy text here'; > aObj2.aProp.text='lorem ipsum dolor'; Here, neither of aObj1 or aObj2 have their own instance of aProp. aObj1.aProp and aObj2.aProp point to the same instance of aProp, the one on the prototype. It's therefore expected that changing one of its properties (text) affects both objects.
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago18 years ago
Resolution: --- → INVALID
(In reply to comment #6) > Blake, it's not quite true what you're saying. The prototype has to be exactly > what its name says: a prototype. Is not a common object that instances of a > class share but rather inherit. Right, property lookups on an object delegate to the object's prototype. > aObj1.bProp = 20; > aObj2.bProp = 40; You're cheating here! Now, you're setting bProp directly on aObj1 and aObj2. In this case, you're overriding (also known as shadowing) the property that's shared on the prototype on each of the objects. This way, when you look up bProp on these objects, you're not getting the version on the prototype, you're getting the version on each object. In the first case, when you get aObj1.aProp, we look up what aProp is on the prototype and return that value (which is an object). Notice that since both aObj1 and aObj2 shared the same prototype (though the objects themselves are not the same), the object that's returned is actually the same object, and mutating it mutates it for both aObj1 and aObj2.
You need to log in before you can comment on or make changes to this bug.