with{this} doesn't work - why?

D

DB McGee

AV said:
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);
 
E

Evertjan.

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>
 
A

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
 
D

DB McGee

Evertjan. said:
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?
 
E

Evertjan.

DB McGee wrote on 09 jan 2004 in comp.lang.javascript:
Could you explain this - what exactly does 'return window' do in this
case?

Does it work for you ?

window is the global object.
 
A

Alliss

DB McGee said:
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"
 
L

Lasse Reichstein Nielsen

DB McGee said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
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)
 
A

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 said:
DB McGee said:
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
 
A

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
 
D

Douglas Crockford

any idea why the following code doesn't work?
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
 
T

Thomas 'PointedEars' Lahn

AV said:
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
 
T

Thomas 'PointedEars' Lahn

AV said:
yes that would fix the script, yet it requires two statements:

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
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
 
D

Douglas Crockford

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
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top