Is there such a thing as this.value ?

M

Martin

I want to create a funtion that will be called from the onChange event
of an input that will prompt the user if the entered value is out of
range. This function will be called from many different inputs.

Is there a way I can get the value (inside the function) without
having to explicitly pass it to the function. IOW, is there someway
that the function can know which input called it and extract that
input's current value? Maybe something like: "this.value" ?
 
T

Thomas 'PointedEars' Lahn

Martin said:
I want to create a funtion that will be called from the onChange event
of an input that will prompt the user if the entered value is out of
range. This function will be called from many different inputs.

Don't. Your approach will likely leave the user trapped in the form, which
goes against standing accessibility guidelines, and, in some instances,
legislation.

What you can and SHOULD do is to use the DOM to display information next to
the offending control (or only mark it somehow, and display collected
information about the errors well visible on top of the form or near the
submit button), and cancel the form's submit even if the form has any
errors.
Is there a way I can get the value (inside the function) without
having to explicitly pass it to the function. IOW, is there someway
that the function can know which input called it and extract that
input's current value? Maybe something like: "this.value" ?

Yes.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Martin said:
I want to create a funtion that will be called from the onChange event
of an input that will prompt the user if the entered value is out of
range. This function will be called from many different inputs.

Don't. Your approach will likely leave the user trapped in the form, which
goes against standing accessibility guidelines, and, in some instances,
legislation.

What you can and SHOULD do is to use the DOM to display information next to
the offending control (or only mark it somehow, and display collected
information about the errors well visible on top of the form or near the
submit button), and cancel the form's `submit' event if the form has any
errors.
Is there a way I can get the value (inside the function) without
having to explicitly pass it to the function. IOW, is there someway
that the function can know which input called it and extract that
input's current value? Maybe something like: "this.value" ?

Yes.


PointedEars
 
J

Jukka K. Korpela

I want to create a funtion that will be called from the onChange event
of an input that will prompt the user if the entered value is out of
range. This function will be called from many different inputs.

Then you can use onchange="foo(this)" in those input elements, and the
parameter to foo can then be used to retrieve the value.

The answer to the question you asked in the Subject line is that yes,
there is such a thing, when the variable this has an object value and
the object has a field named "value".
Is there a way I can get the value (inside the function) without
having to explicitly pass it to the function.

What's wrong with passing the value you need?
 
T

Thomas 'PointedEars' Lahn

Jukka said:
Then you can use onchange="foo(this)" in those input elements, and the
parameter to foo can then be used to retrieve the value.

The answer to the question you asked in the Subject line is that yes,
there is such a thing, when the variable this has an object value and
the object has a field named "value".

AISB, `this' is a reserved word ([ES5], section 7.6.1.1); it cannot be
produced by the Identifier production (ibid., 7.6), so it MUST NOT and
therefore cannot be a variable identifier (ibid., 12.2).

Further, `this' refers to an object by default. It only does not refer to
an object when the caller provides a non-Object value (where Object is to be
understood in terms of type, not [[Class]]), the implementation is fully
ES5-conforming, and strict mode has been enabled (ibid., 10.4.3).
What's wrong with passing the value you need?

Passing around non-Reference values can be inefficient.


PointedEars
___________
[ES5]
<http://www.ecma-international.org/publications/standards/Ecma-262.htm>
 
J

Jukka K. Korpela

2011-06-24 22:02 said:
AISB, `this' is a reserved word ([ES5], section 7.6.1.1);

It is refreshing to troll the trolls, though it may confuse novices.
Passing around non-Reference values can be inefficient.

People who care about inefficiency of trivial operations in client-side
JavaScript should really get a better hobby. If they can.
 
T

Thomas 'PointedEars' Lahn

Jukka said:
2011-06-24 22:02 said:
AISB, `this' is a reserved word ([ES5], section 7.6.1.1);

It is refreshing to troll the trolls, though it may confuse novices.

Am I to understand that you deliberately posted misinformation here so
that you would need to be corrected by the people you call "troll"?
People who care about inefficiency of trivial operations in client-side
JavaScript should really get a better hobby. If they can.

Your logic is not only flawed, it is non-existent. If you think this could
convince *anyone*, it is you who really should get another hobby.

Get better.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Passing around non-Reference values can be inefficient.

What is a non-Reference value?
(Or maybe more relevant: How do you pass a Reference value to anything?)
/L
 
L

Lasse Reichstein Nielsen

Martin said:
I want to create a funtion that will be called from the onChange event

You say "called from", not "called as". I assume that this means that
your function isn't assigned directly to handle the event (because
in that case you get the input element as "this" value during execution
of the handler function - unless you use the IE specific attachEvent,
ofcourse).
of an input that will prompt the user if the entered value is out of
range. This function will be called from many different inputs.

Is there a way I can get the value (inside the function) without
having to explicitly pass it to the function.

I assume you mean the value of the "value" property of the input
element in question.

In that case ... neh.
Yes, you can avoid passing the value explicitly, if you pass it implicitly
by passing something else explicity which holds the value or a reference to
and object that can be used to get the value.

No, you can't avoid passing something explicitly that is used to get
the value.
In *some* browsers you can, because the event is globally available
as window.event. However, that's not part of the DOM Event specification,
and in other browsers, most notably Firefox, that won't work.
IOW, is there someway
that the function can know which input called it and extract that
input's current value? Maybe something like: "this.value" ?

If you do:
inputElement.addEventListener("change", myFunction, false);
to attach the function (or even the old-style
inputElement.onchange = myFunction;
but I recommend the former where available), then "this" will provide
the inputElement during execution of the function.

Old IE's doesn't have addEventListener, so there you'll either have
to use the ".onchange=" strategy, or do some wrapping of the function
before passing it to attachEvent.

If you call the function from an intrinsic event handler, i.e.,
<input ... onchange="myFunction();">
then no. There won't be a way that works everywhere, but in that case,
don't call it like that. Use:
<input ... onchange="myFunction.call(this);">
instead, to explicitly pass the input element as the "this" value,
or just
<input ... onchange="myFunction(this);">
to just pass it as an argument. It must be build to expect the value
anyway, so which way it expects to get it shouldn't be important.

Personally, I'd use addEventListener.
/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
What is a non-Reference value?
(Or maybe more relevant: How do you pass a Reference value to anything?)

You are correct, I should have said non-Object values, i.e. primitive
values. I have used "non-Reference" value to emphasize that it should be
more efficient to pass references to objects to methods instead of primitive
values of properties of objects.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
I should have said non-Object values, i.e. primitive
values. I have used "non-Reference" value to emphasize that it should be
more efficient to pass references to objects to methods instead of primitive
values of properties of objects.

If you need to pass more than one value, it's probably more efficient to
pass a reference to an existing object where the values can be accessed.

I doubt it's any faster to pass an object than to pass a single non-object
value, though.

/L
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 26 jun 2011 in comp.lang.javascript:
I doubt it's any faster to pass an object than to pass a single
non-object value, though.

Should be just as fast,
since you cannot and do not pass an object,
only it's "single" reference-value.
 
S

Stanimir Stamenkov

Sun, 26 Jun 2011 00:42:18 +0200, /Lasse Reichstein Nielsen/:
If you need to pass more than one value, it's probably more efficient to
pass a reference to an existing object where the values can be accessed.

I doubt it's any faster to pass an object than to pass a single non-object
value, though.

May be passing an object reference and not the values of two of its
properties, for example, is some nanosecond faster, but then if one
has to access those property values repeatedly (in the invoked
method), they are better assigned to local variables, which should
be the same as passing them as parameters.
 
E

Evertjan.

Stanimir Stamenkov wrote on 26 jun 2011 in comp.lang.javascript:
Sun, 26 Jun 2011 00:42:18 +0200, /Lasse Reichstein Nielsen/:

May be passing an object reference and not the values of two of its
properties, for example, is some nanosecond faster, but then if one
has to access those property values repeatedly (in the invoked
method), they are better assigned to local variables, which should
be the same as passing them as parameters.

The scope difference of passing by reference or by value when used
diligently, can save far more time and code lines than the simplistic
implementation referred to here.
 
S

Stanimir Stamenkov

26 Jun 2011 08:10:04 GMT, /Evertjan./:
The scope difference of passing by reference or by value when used
diligently, can save far more time and code lines than the simplistic
implementation referred to here.

As far as I'm aware, in JS one always passes values - either
references to objects, or primitive values. With pass-by-reference
I understand:

function foo(a) {
a += 1;
}

function bar() {
var a = 5;
foo(a);
// If passed by reference, 'a' should be 6 at this point.
}

Is there such thing in JavaScript?
 
E

Evertjan.

Stanimir Stamenkov wrote on 26 jun 2011 in comp.lang.javascript:
26 Jun 2011 08:10:04 GMT, /Evertjan./:


As far as I'm aware, in JS one always passes values - either
references to objects, or primitive values. With pass-by-reference
I understand:

No, your understanding is wrong.

Variables are passed by value,
[meaning that only the value is passed]
objects are passed by reference,
[meaning thet the object-pointer is passed.]

============= by value =========
var a = 3; // 3
alert(a);
f(a);
alert(a); // 3 <-- !!!

function f(x) {
x++;
alert(x); // 4
};
================================

========== by reference ========
var obj = {a:3};
alert(obj.a); // 3
f(obj);
alert(obj.a); // 4 <-- !!!

function f(x) {
x.a++;
alert(x.a); // 4
};
===============================
 
L

Lasse Reichstein Nielsen

Stanimir Stamenkov said:
26 Jun 2011 08:10:04 GMT, /Evertjan./:


As far as I'm aware, in JS one always passes values - either
references to objects, or primitive values. With pass-by-reference I
understand:
[example]

You are correct.
Is there such thing in JavaScript?

No. Javascript/ECMAScript is purely pass-by-value.

/L
 
S

Stanimir Stamenkov

26 Jun 2011 09:25:53 GMT, /Evertjan./:
Stanimir Stamenkov wrote on 26 jun 2011 in comp.lang.javascript:


No, your understanding is wrong.

Seems I'm not the only one, then - Lasse Reichstein Nielsen agrees
with me in another reply. (see further below)
Variables are passed by value,
[meaning that only the value is passed]
objects are passed by reference,
[meaning thet the object-pointer is passed.]

============= by value =========
var a = 3; // 3
alert(a);
f(a);
alert(a); // 3<-- !!!

function f(x) {
x++;
alert(x); // 4
};
================================

========== by reference ========
var obj = {a:3};
alert(obj.a); // 3
f(obj);
alert(obj.a); // 4<-- !!!

function f(x) {
x.a++;
alert(x.a); // 4
};
===============================

Your last example appears wrong or at least misleading. You still
pass the value of the variable 'obj', which happens to be a
reference to an object, by value. To pass the variable by reference
would mean you can "magically" modify the variable to point to
another object, like:

function foo(obj) {
obj = { a : 4 };
}

functoin bar() {
var obj = { a : 3 };
foo(obj);
// If 'obj' has been passed by reference to foo(),
// 'obj.a' should evaluate to 4.
}
 
L

Lasse Reichstein Nielsen

Evertjan. said:
Stanimir Stamenkov wrote on 26 jun 2011 in comp.lang.javascript:


No, your understanding is wrong.

No, it is correct.
Variables are passed by value,
[meaning that only the value is passed]
objects are passed by reference,
[meaning thet the object-pointer is passed.]

That's not what "pass by reference" means (in the sense used in
computer science, which was where the terminology was invented). In
pass-py-reference, you pass a reference to a *variable*, not to a
*value*.
See: http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference

Javascript just has the passing-by-value of a "reference" value.
You can't pass a reference to a variable.

What Javascript does have is also known as "call-by-sharing" or
(more rarely "call-by-object"). See the same wikipedia page.

/L
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 26 jun 2011 in comp.lang.javascript:
Evertjan. said:
Stanimir Stamenkov wrote on 26 jun 2011 in comp.lang.javascript:


No, your understanding is wrong.

No, it is correct.
Variables are passed by value,
[meaning that only the value is passed]
objects are passed by reference,
[meaning thet the object-pointer is passed.]

That's not what "pass by reference" means (in the sense used in
computer science, which was where the terminology was invented). In
pass-py-reference, you pass a reference to a *variable*, not to a
*value*.

As I showed above. You are not adding anything.
See: http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference

Javascript just has the passing-by-value of a "reference" value.
You can't pass a reference to a variable.

As I showed above. You are not adding anything.
What Javascript does have is also known as "call-by-sharing" or
(more rarely "call-by-object"). See the same wikipedia page.

That you ALSO can call it by another name is fine.

I think your argumentation is therefore utter nonsense.

I will expand my above reasoning:

Function arguments containing a variable
only pass IT'S CONTAINING VALUE
and NOT THE REFEREBENCE TO THE VARIABLE.
So the argument name is a new variable with local scope.
This is called "passing by value".

While function arguments containing an object
pass the REFERENCE TO THAT OBJECT,
and NOT IT'S CONTENTS.
So the argument name is just another name of the object
where only the name has local scope.
This is called "passing by reference".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top