Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24671
| 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 21:03 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <4155262.DTQR67hhvp@PointedEars.de> (permalink) |
| References | <12u13ppz1f252.63eqsi8uwd9r.dlg@40tude.net> <5180532.WuG82CC6N6@PointedEars.de> <4dy7dfnuv1vq$.1i9vecn58t2n8$.dlg@40tude.net> |
JJ wrote:
> On Sat, 07 Jun 2014 13:48:14 +0200, Thomas 'PointedEars' Lahn wrote:
>> JJ <duh@nah.meh> wrote:
>> ^^^^^^^^^^^^^^^^
>> <http://www.interhack.net/pubs/munging-harmful/>. Please fix.
>
> What are the options other than using my real email address?
Your reasonable options are discussed in that document.
I recommend to use the address of an aggressively-filtered, seldom-read (but
*read*) mailbox for the From header field, and the address of a more often
read mailbox for the Reply-To header field. That is what I do.
>>> 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.
>
> So, does this means there's only one "val" variable in that context?
There is only one “val” variable per execution context. However, the point
here is that by overwriting the prototype’s property value in the
constructor you are binding the identifier reference in the prototype method
always to *that* variable, i. e. that of the execution context of the last
constructor call.
>>> 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;
>> };
>> }
>
> What about the prototypal Number object, where:
>
> (new Number()).toFixed === Number.prototype.toFixed
>
> How to implement that?
The expression evaluates to true because the left-hand side is resolved
to Number.prototype.toFixed through the prototype chain.
("toFixed" in (new Number())) === true
but
(new Number()).hasOwnProperty("toFixed") === false
(Besides, new Number(…) should be avoided.)
As for exactly the same functionality as the built-in object, I do not think
that is possible. A user-defined prototype method needs a publicly
accessible property on the instance. You could only have a public prototype
method calling a "privileged" instance method which accesses the context-
local variable aka the "private property". The following comes close:
"use strict";
Object.defineProperty(this, "Num", {
configurable: true,
/* not enumerable */
writable: true,
value: function Num (value) {
var has_args = (arguments.length > 0);
if (!(this instanceof Num))
{
if (has_args)
{
return +value;
}
return 0;
}
var _primitive_value = (has_args ? +value : 0);
Object.defineProperty(this, "_getPrimitiveValue", {
/* not configurable, not enumerable, not writable */
value: function () {
return _primitive_value;
}
});
}
});
Object.defineProperty(this.Num, "prototype", {
/* not configurable, not enumerable, not writable */
value: (function (_global) {
var prototype = new _global.Num();
Object.defineProperties(prototype, {
/* … */
toFixed: {
configurable: true,
writable: true,
value: function (precision) {
var primitive_value = this._getPrimitiveValue();
/* achieve precision */
return primitive_value;
}
},
toString: {
configurable: true,
writable: true,
value: function (radix) {
return this._getPrimitiveValue().toString(radix);
}
},
valueOf: {
configurable: true,
writable: true,
value: function () {
return this._getPrimitiveValue();
}
}
});
return prototype;
}(this))
});
See also <http://ecma-international.org/ecma-262/5.1/#sec-15.7>.
--
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