473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

with{this} doesn't work - why?

AV
Hallo

any idea why the following code doesn't work?

//////////////////////////////////
function myfunc(){
with(this){
prop="hallo world";
}
}

var foo=new myfunc();
alert(foo.prop)
//////////////////////////////////

I want to make a 'with' statement work with the keyword 'this' but even that
simple snippet apparently fails. Any idea?
thank you in advance
Jul 20 '05 #1
15 1554
AV wrote:
Hallo

any idea why the following code doesn't work?

//////////////////////////////////
function myfunc(){
with(this){
prop="hallo world";
}
}

var foo=new myfunc();
alert(foo.prop)
//////////////////////////////////

I want to make a 'with' statement work with the keyword 'this' but
even that simple snippet apparently fails. Any idea?
thank you in advance


Is there a specific reason you don't want to do it this way??

function myfunc() {
this.prop = "hallo world";
}

var foo = new myfunc();
alert(foo.prop);
Jul 20 '05 #2
AV wrote on 09 jan 2004 in comp.lang.javascript:
any idea why the following code doesn't work?

//////////////////////////////////
function myfunc(){
with(this){
prop="hallo world";
}
}

var foo=new myfunc();
alert(foo.prop)
//////////////////////////////////


<script>

function myfunc(){
with(this){
prop="hallo world";
}
return window
}

var foo=new myfunc();
alert(foo.prop)

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #3
Evertjan. wrote:
AV wrote on 09 jan 2004 in comp.lang.javascript:

<script>

function myfunc(){
with(this){
prop="hallo world";
}
return window
}

var foo=new myfunc();
alert(foo.prop)

</script>


Could you explain this - what exactly does 'return window' do in this case?
Jul 20 '05 #4
DB McGee wrote on 09 jan 2004 in comp.lang.javascript:
Evertjan. wrote:
AV wrote on 09 jan 2004 in comp.lang.javascript:

<script>

function myfunc(){
with(this){
prop="hallo world";
}
return window
}

var foo=new myfunc();
alert(foo.prop)

</script>


Could you explain this - what exactly does 'return window' do in this
case?


Does it work for you ?

window is the global object.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #5
Evertjan. wrote:
<script>

function myfunc(){
with(this){
prop="hallo world";
}
return window
}

var foo=new myfunc();
alert(foo.prop)

</script>


Could you explain this - what exactly does 'return window' do in this
case?


Does it work for you ?

window is the global object.


Yes it does work - just trying to understand the particulars of why it does :)
Jul 20 '05 #6

"DB McGee" <no*****@noreply.com> wrote in message
news:pJ*******************@news01.bloor.is.net.cab le.rogers.com...
Evertjan. wrote:
<script>

function myfunc(){
with(this){
prop="hallo world";
}
return window
}

var foo=new myfunc();
alert(foo.prop)

</script>

Could you explain this - what exactly does 'return window' do in this
case?


Does it work for you ?

window is the global object.


Yes it does work - just trying to understand the particulars of why it

does :)

The thing I find interesting is that:

function myfunc(){
with(this){
prop="hallo world";
}
}
var foo=new myfunc();
alert(foo.prop); returns undefined
but
alert(prop); returns "hallo world"
Jul 20 '05 #7
"DB McGee" <no*****@noreply.com> writes:
Yes it does work - just trying to understand the particulars of why it does :)


The trick is that

var obj = new Object();
with (obj) {
prop = 42;
}
alert([obj.prop,window.prop]);

alerts "undefined,42". That is, the property is set on the global object,
not on the object of the "with".

Writing
with (obj) {prop = 42;}
only changes the property "prop" of obj if it already has that property.
Using "with" doesn't mean that new variables are created in that object.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
AV
Of course there is!
But if I explain it to you it takes 15 minutes. But sure there is.

"DB McGee" <no*****@noreply.com> ha scritto nel messaggio
news:xt********************@news01.bloor.is.net.ca ble.rogers.com...
AV wrote:
Hallo

any idea why the following code doesn't work?

//////////////////////////////////
function myfunc(){
with(this){
prop="hallo world";
}
}

var foo=new myfunc();
alert(foo.prop)
//////////////////////////////////

I want to make a 'with' statement work with the keyword 'this' but
even that simple snippet apparently fails. Any idea?
thank you in advance


Is there a specific reason you don't want to do it this way??

function myfunc() {
this.prop = "hallo world";
}

var foo = new myfunc();
alert(foo.prop);

Jul 20 '05 #9
AV
Any ide why it doesn't work with the keyword this? I am interested as well
as all of you about the theoretical side (that return window is mysterious
to me. That the property got assigned to the window was already apparent to
me because prop=something was stated in the code, and if that was not
attached to keyword 'this', then it was obviously attahced to window). yet I
want to understand why I cannot make a with() statement work with keyword
this.
If what happens is that all gets attached to the window, what I wanted to
achieve is lost.

I have a very long set of properties that i want to encapsulate in a
javascript "class" so to avoid potential name conflicts in a document. Let's
say

//pseudocode
property50=50; property 78=78;

//I need to encapsulate all of them in a class, possibly without
painstakingly adding 'this' before all of them...
"Lasse Reichstein Nielsen" <lr*@hotpop.com> ha scritto nel messaggio
news:8y**********@hotpop.com...
"DB McGee" <no*****@noreply.com> writes:
Yes it does work - just trying to understand the particulars of why it
does :)
The trick is that

var obj = new Object();
with (obj) {
prop = 42;
}
alert([obj.prop,window.prop]);

alerts "undefined,42". That is, the property is set on the global object,
not on the object of the "with".

Writing
with (obj) {prop = 42;}
only changes the property "prop" of obj if it already has that property.
Using "with" doesn't mean that new variables are created in that object.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #10
JRS: In article <bt**********@lacerta.tiscalinet.it>, seen in
news:comp.lang.javascript, AV <NO****@hotmail.com> posted at Fri, 9 Jan
2004 21:53:17 :-
any idea why the following code doesn't work?

//////////////////////////////////
function myfunc(){
with(this){
prop="hallo world";
}
}

var foo=new myfunc();
alert(foo.prop)
//////////////////////////////////

I want to make a 'with' statement work with the keyword 'this' but even that
simple snippet apparently fails. Any idea?


Others seem not to have noticed the "Why" component of the question.

The effect of with (this) is that this is searched to see if it
has a prop then any enclosing scopes are searched until, ultimately,
there is definitively no such thing; then, prop is created in the
outermost scope. Then, the string is assigned to prop .

Making it work is now obvious; use with only to access existing
components of its "argument".
The following corrects your code and adjusts your spelling :

function myfunc(){
with(this){
this.prop="hallo world";
prop = "hello world";
}
}

var foo=new myfunc();
alert(foo.prop)

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #11
AV
Hallo John (and all)

yes that would fix the script, yet it requires two statements:
this.prop="hallo world";
prop = "hello world";


which is precisely what I want to avoid: having to prepend this to all the
variables _manually_.

Ok, I describe better the problem I have:

I have a dhtml system fully developed. As every dhtml system, it starts with
a lot of variables to cover the different dhtml objects that any document
may imply, addressing them accordingly to browsers differences.
Instances:

-----------
offsetX=(typeof(pageXOffset)!="undefined")?
"pageXOffset":"document.body.scrollLeft";
offsetY=(typeof(pageYOffset)!="undefined")?
"pageYOffset":"document.body.scrollTop";
innerW=(typeof(innerWidth)!="undefined")?
innerWidth:document.body.clientWidth;
innerH=(typeof(innerHeight)!="undefined")?
innerHeight:document.body.clientHeight;
-----------

and all the paraphernalia like that.
You can have an idea what the system implies as far as the amount of
involved vars are at:
http://www.unitedscripters.com/scripts/dhtml4.html
yet viewing the file is not necessary: you can believ my word they are a lot
of variables.

Now, I was planning to encapsulate all that system (sadly already fully
developed. Yet nearly all developers make the same "mistake": with dhtml
they never think, given the amount of vars every dhtml approach requires, it
would be better to start since the beginning incapsulating them. But that's
the problem every developement implies I guess: your project always arrives
at a stage, if you carry it along long enough, when it starts having sort of
a life of its own, not envisioned at the beginning no matter how hard you
"cunningly" projected, and new exigencies arise in spite of ourselves) .

What I need would then be:
1) withdrwing from the window all the assignements of the variables, in
order to assign all of them to _one_ class, thus affecting the window with
_one_ variable dependancy only.
2) achieving that possibly avoiding prepending the 'this' keyword to every
single statement - thence the idea of the with() "function".

Yet if what with() does is to go on either appending to the window or
requiring me to write 'this' before every statement, the purpose is vanished
and my only chance would then be to rewrite the whole of the codes
encapsualting and adding 'this' to every statement.

So, no way to force with() to consider 'this' as a reference to the current
object instead than to the window. Or, in other terms, any way to force
with() to start checking what this might be from the bottom of the ladder
(the current object) instead than from the top (the window)?

Thank you a lot
Alberto
Jul 20 '05 #12
> >any idea why the following code doesn't work?

//////////////////////////////////
function myfunc(){
with(this){
prop="hallo world";
}
} I want to make a 'with' statement work with the keyword 'this' but even that
simple snippet apparently fails. Any idea?


Others seem not to have noticed the "Why" component of the question.

The effect of with (this) is that this is searched to see if it
has a prop then any enclosing scopes are searched until, ultimately,
there is definitively no such thing; then, prop is created in the
outermost scope. Then, the string is assigned to prop .

Making it work is now obvious; use with only to access existing
components of its "argument".


Quite right. The uncertainty around the action of with makes it an unsafe
feature, best avoided. In this case,

this.prop = "hullo world";

is concise and non-ambiguous. For more complex formulations, a var is better
than a with :

var c = document.body.children[document.body.children.length - 1];
c.innerHTML = 'blah';

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #13
AV wrote:
Any ide why it doesn't work with the keyword this?


Lasse just explained it, and if you would both have not top-posted
and have trimmed your quotes, you would have noticed it.

The "with" statement does not work for assignments to properties
that the referenced object does not already have, instead they are
newly *created* as properties of the global object. It is a scope
problem not restricted to "this". Reasonable "with" usage is
restricted, so it is simply bad style and thus it causes "deprecated
with statement usage" warnings in recent Mozillas.

BTW, "this" is not only a keyword and a reserved word, it is a
reference to the calling object, which is the global object in
global functions/methods (which is nothing to do with that the
created property's owner is the global object!) and to the
object that triggered the event in event listeners.
PointedEars
Jul 20 '05 #14
AV wrote:
yes that would fix the script, yet it requires two statements:
this.prop="hallo world";
prop = "hello world";

No, it does not.

this.prop = "hello world";

is sufficient and correct, while your code will possibly create
two properties -- one of the calling object and one of the global object.
which is precisely what I want to avoid: having to prepend this to all the
variables _manually_.
You do not have to do it manually. Use RegExp find-and-replace:

s/^(\b*)\b?(\w)/\1this.\2/gm

(This is a quickhack, as usual make a backup before you apply it!)
I have a dhtml system fully developed. As every dhtml system, it starts with
a lot of variables to cover the different dhtml objects that any document
may imply, addressing them accordingly to browsers differences.
It does not make sense to spoil the namespace with global variables that
are possibly never used. Instead, it has proven to be more efficient to
test for what is going to be used in the next instant. See also
<http://pointedears.de.vu/scripts/test/whatami>.
Yet if what with() does is to go on either appending to the window or
requiring me to write 'this' before every statement, the purpose is vanished
Sorry, I cannot see that.
and my only chance would then be to rewrite the whole of the codes
encapsualting and adding 'this' to every statement.
Yes, and you would have working code then.
So, no way to force with() to consider 'this' as a reference to the current
object instead than to the window.


No, because "this" is not the problem here. "with" is.
PointedEars
Jul 20 '05 #15
> The "with" statement does not work for assignments to properties
that the referenced object does not already have, instead they are
newly *created* as properties of the global object. It is a scope
problem not restricted to "this". Reasonable "with" usage is
restricted, so it is simply bad style and thus it causes "deprecated
with statement usage" warnings in recent Mozillas.

BTW, "this" is not only a keyword and a reserved word, it is a
reference to the calling object, which is the global object in
global functions/methods (which is nothing to do with that the
created property's owner is the global object!) and to the
object that triggered the event in event listeners.


Well said. jslint, the JavaScript Verifier, rejects the with statement.

http://www.crockford.com/javascript/lint.html
Jul 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rob Meade | last post by:
Hi all, I found an article on how to write a Windows service here: http://www.dotnetbips.com/displayarticle.aspx?id=178 It was pretty much what I wanted, I want the service to scan a...
0
by: TN Bella | last post by:
Hi, I am trying to get my compare validator to fire properly...Since I have panels the validator wouldn't work properly, the app would fire right but would insert the data regardless and the...
4
by: Maziar Aflatoun | last post by:
Hi everyone, I am working on the 'Delete' section of my program. What I need to do is query my database and for every ID that it finds I want to remove a file+ID.jpg from my file folder and...
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
1
by: objectmodelol | last post by:
I just switched from MS SQL 2000/2005 to MySql. What's wrong with this stored procedure: DELIMITER $$ DROP PROCEDURE IF EXISTS `listing`.`SaveUser` $$ CREATE DEFINER=`root`@`localhost`...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
5
by: Vols | last post by:
class A{ public: int x; }; class B : public A{ public: int y; }; void foo()
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.