WITH (this) {
a = 5
}
I'm finding that this.a = 5 will come from this expression. I would
have thought that the search for a would come up undefined and leave it
at that.
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ? 27 1888
SamJs wrote:
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?
That is irrelevant to with() construct. If you assign a value to a
non-existing object property, the engine creates new property first and
then assign to it.
"SamJs" <Sa*******@gmai l.comwrote:
WITH (this) {
a = 5
}
I'm finding that this.a = 5 will come from this expression. I would
have thought that the search for a would come up undefined and leave it
at that.
How strange, I would have thought you would get a syntax error from this
expression. Javascript doesn't have a 'WITH' statement (although it does
have a 'with' statement).
>
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?
Try this and you will find that assigning to 'a' doesn't create an
attribute on this:
f = function() {
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g. a+", window.a:"+wind ow.a);
// displays: g.a:undefined, window.a:42
but the following code will update the attribute on 'this':
window.a = undefined;
f = function() {
this.a = 'hello';
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g. a+", window.a:"+wind ow.a);
// displays: g.a:42, window.a:undefi ned
SamJs wrote:
WITH (this) {
a = 5
}
I'm finding that this.a = 5 will come from this expression.
Syntax error aside, that observation depends on the value of the this
operator. The this operator may, and often does, refer to the global object.
As long as there is no variable, a, within the scope chain, the
assignment expression above will create a new property on the global
object. If the this operator does indeed refer to that object, then the
expression
this.a
will evaluate to the number 5.
Compare to:
var object = {
method : function() {
with (this) {
a = 5;
}
alert(this.a);
}
};
object.method() ;
Here, the this operator refers to the object assigned to the variable,
object. As before, the assignment expression within the with statement
creates a property on the global object, but because the this operator
value is different, the expression evaluates to undefined.
[snip]
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?
Yes: it never happens. Only existing properties can be read or assigned.
Mike
Duncan Booth wrote:
"SamJs" <Sa*******@gmai l.comwrote:
[...]
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?
Try this and you will find that assigning to 'a' doesn't create an
attribute on this:
Yes it does...
>
f = function() {
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g. a+", window.a:"+wind ow.a);
// displays: g.a:undefined, window.a:42
Because the value of this was the window object, so an a property was
added to window and assigned a value of 42.
but the following code will update the attribute on 'this':
window.a = undefined;
f = function() {
this.a = 'hello';
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g. a+", window.a:"+wind ow.a);
// displays: g.a:42, window.a:undefi ned
Because when you call f as a constructor, its this value is set to the
newly created object: this.<propertyN ameassigns properties to the new
object.
--
Rob
"RobG" <rg***@iinet.ne t.auwrote:
Duncan Booth wrote:
>"SamJs" <Sa*******@gmai l.comwrote:
[...]
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ? Try this and you will find that assigning to 'a' doesn't create an attribute on this:
Yes it does...
Assignment never creates a new attribute on the object referred to in the
'with' statement.
>> f = function() { with(this) { a = 42; } } var g = new f(); alert("g.a:"+g .a+", window.a:"+wind ow.a); // displays: g.a:undefined, window.a:42
Because the value of this was the window object, so an a property was
added to window and assigned a value of 42.
Hello? Did you read the code?
f is called with the new operator. The value of 'this' inside the function
is the newly created object, not the window object. Neither has an 'a'
attribute to the assignment creates a new value on the global object (which
in this case happens to be the window object).
>
>but the following code will update the attribute on 'this':
window.a = undefined; f = function() { this.a = 'hello'; with(this) { a = 42; } } var g = new f(); alert("g.a:"+g .a+", window.a:"+wind ow.a); // displays: g.a:42, window.a:undefi ned
Because when you call f as a constructor, its this value is set to the
newly created object: this.<propertyN ameassigns properties to the new
object.
Yes, 'this' is the same as in the first example. The difference this time
though is that this.a already exists so the assignment to plain 'a' updates
the existing attribute.
Duncan Booth wrote:
Assignment never creates a new attribute on the object referred to in the
'with' statement.
Welcome to JavaScript :-) and you are wrong as it's already pointed out
:-(
If property doesn't exist yet, it will be conveniently created for you.
>
f = function() {
with(this) {
a = 42;
}
}
var g = new f();
alert("g.a:"+g. a+", window.a:"+wind ow.a);
// displays: g.a:undefined, window.a:42
Because the value of this was the window object, so an a property was
added to window and assigned a value of 42.
Hello? Did you read the code?
It seems that you (as well as OP) are semi-mesmerized by with()
construct so presuming some hidden magic out of it: while it's nothing
but a shortcut tool.
with (this) {a = 'foo';}
programmaticall y is absolutely the same as
this.a = 'foo';
Try this:
function f() {
this.a = foo;
}
f();
alert(window.a) ; // 'foo'
var foo = new f();
alert(foo.a) // 'foo'
1] If property doesn't exist yet, it will be created during the
assignment.
2] The call context is everything in JavaScript
Duncan Booth wrote:
"SamJs" <Sa*******@gmai l.comwrote:
Is JavaScript explicit about the behavior of creating variables on a
WITH (object) ?
[...]
Assignment never creates a new attribute on the object referred to in the
'with' statement.
So Mike said - maybe I should have believed him. :-)
[...]
Did you read the code?
Yes, but it didn't sink in at the time. I don't use with, and probably
never will - thanks to this discussion I have a better idea of how it
works.
--
Rob
VK wrote:
Assignment never creates a new attribute on the object referred to in the
'with' statement.
Humply swallow my words back
<script>
function f() {
with(this) {
a = 'foo';
}
}
f();
alert(window.a) ; // foo
var foo = new f();
alert(foo.a); // undefined
</script>
Interesting...
VK wrote:
VK wrote:
>Duncan Booth wrote:
>>Assignment never creates a new attribute on the object referred to in the 'with' statement.
Welcome to JavaScript :-) and you are wrong as it's already pointed
out :-(
If property doesn't exist yet, it will be conveniently created for you.
Humply swallow my words back
<script>
function f() {
with(this) {
a = 'foo';
}
}
f();
alert(window.a) ; // foo
var foo = new f();
alert(foo.a); // undefined
</script>
Interesting...
As you have never managed to grasp the difference between scope chain
Identifier resolution and property accessor resolution you may find
seeing the expected result "interestin g". In reality all you are doing
is again demonstrating that your ignorance of javascript, and fictional
perceptions of it lead you to repeatedly making false statements about
javascript, and wasting everyone's time reading them and correcting the
more coherent ones.
Richard. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Richard Galli |
last post by:
I want viewers to compare state laws on a single subject.
Imagine a three-column table with a drop-down box on the top. A viewer
selects a state from the list, and that state's text fills the column below.
The viewer can select states from the drop down lists above the other two
columns as well. If the viewer selects only one, only one column fills. If
the viewer selects two states, two columns fill. Etc.
I could, if appropriate, have...
|
by: Andreas Paasch |
last post by:
I'm having trouble with the implode function and need some help here.
I've been reading at php.net but that didn't solve my problem.
I'm having the below code:
$query = "SELECT top1, top2, top3, top4, top5, top6,
top7, top8, top9, pricesmall, pricelarge
FROM esperia
WHERE productnumber = '$bestil'";
|
by: Georg Gerber |
last post by:
Hello,
I use an Apache-Web-Server 1.3 with PHP 5 under Windows 2000. With a
PHP-script I want to get data from a MS-SQL-Server.
Therefore is within my php.ini:
extension=php_mssql.dll
Within my PHP-script the first relevant line is:
|
by: Mike Henley |
last post by:
I first came across rebol a while ago; it seemed interesting but then
i was put off by its proprietary nature, although the core of the
language is a free download.
Recently however, i can't help but say i was totally impressed. I
needed an open source wikiblog/wikilog, whatever you wanna call it,
basically a hybrid of a blog and a wiki. I checked out snipsnap, which
uses java, it was said on their site to be a clone of vanilla, a...
|
by: Roswitha Schoppe-Jantzen |
last post by:
Hallo
The Class htmlMimeMail (from de.comp.lang.php.* FAQ)
does not work with PHP 4.3.3,
before with PHP 4.2.3 it went well.
If I send a PDF-file with PHP 4.3.3, the mail is send, but the PDF-file is
ruined.
Has anyone an idea?
| |
by: Rustam Bogubaev |
last post by:
Hi,
I have RedHat 9.0 and installed by default Apache+PHP. By default XSLT
Sablotron support is not enabled, so how can I rebuild PHP with xslt
support?
I know that I can rebuild it from sources as described on
http://www.protonicdesign.com/tutorial/sablot_and_php.php, but I want
to do it without uninstalling and breaking present configuration. I
just need append support for XSLT Sablotron.
|
by: Sam |
last post by:
Hi All
I am playing around with the idea of writing a xml based cms, and would like
it to be able to be used on hostings where you do not have the ability to
password protect directories.
Would a simple permissions check at the top of the file be enough to enure
it is not read?
<?
|
by: Craig Keightley |
last post by:
At the moment, i am plying with the GD functions using Jpegs and the Image
Resample function.
I was wondering if there is a way to add a second image over an existing
image.
I am working on a shopping cart system with thumbnail images and I want to
do one of the following depending on which looks better:
Create a "SOLD OUT" Image over the existing image
Turn the image to a Black and White Picture
|
by: Paulie |
last post by:
hello, everybody
i'm pretty new with linux & php, i have some problems:
1- can't make flash work on PHP, i downloaded the files libshw.a and
shw.h, i've copied them in /usr/local/lib & /usr/local/include
the fact is that the command "./configure --with-usf=/usr/local" is
not working, bash says that it's an "unknown command"
why?
|
by: kecebong |
last post by:
I tried to compile php 4.3.3 with gd but it doesn't work, it wasnt show
in phpinfo(). My system is redhat 9 and apache 2.0.47 webserver.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |