Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Object instance's local variable is not separate Date: Sat, 07 Jun 2014 13:48:14 +0200 Organization: PointedEars Software (PES) Lines: 106 Message-ID: <5180532.WuG82CC6N6@PointedEars.de> References: <12u13ppz1f252.63eqsi8uwd9r.dlg@40tude.net> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1402141699 19204 eJwFwYEBwDAEBMCVhPdiHFH2H6F3bjzsAJ3w9Z0Jlsv3OoOWg0rDyMoDr+qeZ6vNgdRVWv8hBhEB (7 Jun 2014 11:48:19 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 7 Jun 2014 11:48:19 +0000 (UTC) User-Agent: KNode/4.12.4 X-User-ID: eJwFwYENwDAIA7CXyIBEPQeq5v8TZncSvCo2q912LAG6Ugp9S9HxPBqwBxN15DeNzcj1d+5L6CxUKO75AT1qFMg= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. wrote: ^^^^^^^^^^^^^^^^ . Please fix. > I wanted to create a class that closely mimic the primitive type clases > such as Number, Boolean, Array, etc. There are no classes. > I think they use a local variable to store the value instead of a > property. I got a sample code from MDN here: > > > > To start with, I change it to below since it needs to be prototypal and be > an instance of the class. > > function Num() { > var val = 0; > Num.prototype.changeTo= function(n) { > val = n; > }; > Num.prototype.value= function() { > return val; > }; > } Your change has b0rked the functionality of the original code. > But the local variable seems no longer separate into each object instance > as tested below. Why is that? > > n1 = new Num(); > n2 = new Num(); After this constructor call, “val” in Num.prototype.value is bound to the local variable in the context of *that* call. > n2.changeTo(1); This changes *that* variable. > console.log(n1.value() + ", " + n2.value()); //1, 1 = fail (should be 0, And both calls return the value of *that* variable, because both objects have the *same* object referred to by Num.prototype next in their prototype chain. A property can only have one value, in this case one reference to a Function instance. Property values are resolved at runtime, through the prototype chain if necessary. > 1) console.log(n1 instanceof Num); //true = ok > console.log(Object.getPrototypeOf(n1).constructor===Num); //true = ok It has to be function Num () { var val = 0; this.changeTo = function(n) { val = n; }; this.value = function() { return val; }; } if objects should not share their property value, and should be something similar to function Num (value) { var _value = 0; this.setValue = function (n) { _value = +n; }; this.getValue = function () { return _value; }; if (typeof Object.defineProperty == "function") { Object.defineProperty(this, "value", { set: this.setValue get: this.getValue, }); } if (arguments.length > 0) { this.setValue(value); } } -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.