dimanche 1 mars 2015

Javascript module pattern - Object.create


A bit of confusion regarding what exactly is going on here.


given this simple code :



stuff = (function(){
function extendFoo(bar)
{
$.extend(this.foo,bar);
}
this.foo = {};

return { foo : foo, extendFoo: extendFoo};
})();


Following this simple operation :



zz = Object.create(stuff);
zz.extendFoo({bar:'bar'});
vv = Object.create(stuff); //foo is still extended(??)


So from what I realized, operations conducted on the returned object form Object.create still affect the prototype of that Object, So when you create a new object, his prototype is changed, thus resulting in a 'modified' version.


This seems wrong on a lot of levels, Can anyone explain what's going on here?


this behavior is not reproducible using the following pattern :



stuff = (function(){

function setFoo(bar)
{
this.foo = bar;
}
var foo;

return { foo : foo, setFoo: setFoo};

})();


So I suspect $.extend is to blame here.


Any input would be great!





Aucun commentaire:

Enregistrer un commentaire