Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24642
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Object instance's local variable is not separate |
| Date | 2014-06-07 13:48 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <5180532.WuG82CC6N6@PointedEars.de> (permalink) |
| References | <12u13ppz1f252.63eqsi8uwd9r.dlg@40tude.net> |
JJ <duh@nah.meh> wrote:
^^^^^^^^^^^^^^^^
<http://www.interhack.net/pubs/munging-harmful/>. 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.
<http://ecma-international.org/ecma-262/5.1/#sec-4.2.1>
> I think they use a local variable to store the value instead of a
> property. I got a sample code from MDN here:
>
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures#Emulating_private_methods_with_closures>
>
> 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: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Object instance's local variable is not separate JJ <duh@nah.meh> - 2014-06-07 16:08 +0700
Re: Object instance's local variable is not separate Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-07 13:48 +0200
Re: Object instance's local variable is not separate JJ <duh@nah.meh> - 2014-06-07 22:35 +0700
Re: Object instance's local variable is not separate Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-07 21:03 +0200
Re: Object instance's local variable is not separate JJ <jj4public@vfemail.net> - 2014-06-09 08:18 +0700
Re: Object instance's local variable is not separate John Harris <niam@jghnorth.org.uk.invalid> - 2014-06-07 15:17 +0100
csiph-web