Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > What is the scope of this new object ?

Reply
Thread Tools

What is the scope of this new object ?

 
 
CqN on cr-48@devChan-13.0.782.43
Guest
Posts: n/a
 
      08-12-2011
In the following code:

function t(){

var d = new Date (string)
var t = d.getTime();

....

delete (d); //IS THIS IMPLCITE by the scope rules of js ???
Essentially redundant?
}
 
Reply With Quote
 
 
 
 
Ghasem
Guest
Posts: n/a
 
      08-13-2011
JavaScript has function scope, i.e. variables are accessible inside the function that defines them. So in your code above, variable "d" is accessible only within function "t". The "delete" statement is not needed.
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      08-13-2011
Ghasem wrote on 13 aug 2011 in comp.lang.javascript:

> JavaScript has function scope, i.e. variables are accessible inside
> the function that defines them. So in your code above, variable "d" is
> accessible only within function "t". The "delete" statement is not
> needed.


"your code above"?

There is on code above!

[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      08-13-2011
12.8.2011 22:01, CqN on cr-48@devChan-13.0.782.43 wrote:

> In the following code:
>
> function t(){
>
> var d = new Date (string)
> var t = d.getTime();
>
> ...
>
> delete (d); //IS THIS IMPLCITE by the scope rules of js ???
> Essentially redundant?
> }


It is a no-operation, and raises a SyntaxError exception in strict mode.
(On Firefox in non-strict mode, it causes a warning.

The delete operator deletes a property of an object. When the operand is
an unqualified variable, as here, it has no effect.

Now, to answer the completely different question you asked in the
Subject line, the answer is that objects do not have scopes. After the
assignment where you create an object, a reference to the object might
be assigned to other variables, like global variables, keeping the
object accessible until the execution of the program. But if such
assignments are not made, the object effectively ceases to exist (and it
may be gargage collected) when the only reference to it vanishes because
it was in a variable local to a function and the function was exited.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      08-13-2011
"CqN on cr-48@devChan-13.0.782.43" <> writes:

> In the following code:
>
> function t(){
>
> var d = new Date (string)
> var t = d.getTime();
>
> ...
>
> delete (d); //IS THIS IMPLCITE by the scope rules of js ???


That depends on what you expect it to do.
It actually does nothing in this case (if you check, it evaluates to false,
which represents the delete failing).
The "delete" operator in Javascript deletes bindings, not objects. You are
trying to delete the "d" variable itself, not the Date object that it refers
to, but variables declared using "var" can't be deleted so nothing happens.

> Essentially redundant?


Fundamentally a no-op, so definitly redundant.

If you want to delete the Date object, all you need to do is to stop
having a reference to it. In this case, the only reference to the Date
object is the "d" variable, and it will stop existing when the function
call that declared it ends. I.e., when the function returns, the Date
object is no longer available, and it will eventually be garbage collected.

If you wanted the Date object to be garbage collectable earlier than
that (more likely in a case where you have a variable referencing a
much larger object, and you are doing a lot of work before returning
from the function), you can overwrite the reference by assigning a new
value to "d", e.g., "d = null;". If d held the only reference to the
object, then the object can no longer be accessed after the assignment,
and the garbag collector may collect it at will.

In this sense, JavaScript is a arbage collected language like Java,
which also has "new", but not "delete".

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-13-2011
Jukka K. Korpela wrote:

> 12.8.2011 22:01, CqN on cr-48@devChan-13.0.782.43 wrote:
>> In the following code:
>>
>> function t(){
>>
>> var d = new Date (string)
>> var t = d.getTime();
>>
>> ...
>>
>> delete (d); //IS THIS IMPLCITE by the scope rules of js ???
>> Essentially redundant?
>> }

>
> It is a no-operation,


No, it is not.

> and raises a SyntaxError exception in strict mode.


Which is additional proof that it is _not_ a no-operation.

> (On Firefox in non-strict mode, it causes a warning.


_In JavaScript_, not "on Firefox".

> The delete operator deletes a property of an object. When the operand is
> an unqualified variable, as here, it has no effect.


(What do you consider to be a qualified variable, then?)

The `delete' operator deletes properties of native objects that do not have
the `[[DontDelete]]' attribute (in ES5, properties that do have the
[[Configurable]] attribute). Identifiers that are declared as variables are
properties of the Variable Object of the execution context they are declard
in and have the `DontDelete' attribute (in ES5, they are References to an
Environment Record binding, respectively).

In addition, in ECMAScript Ed. 5 strict mode an expression cannot be operand
to the `delete' operator if its value "is a direct reference to a variable,
function argument, or function name(11.4.1)" [ES5, Annex C], which follows
from that being "a Reference to an Environment Record binding" there.

*Therefore*, and not for other reasons, the `delete' operation *fails* here.
It is _not_ a no-operation.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      08-13-2011
Thomas 'PointedEars' Lahn <> writes:

> Jukka K. Korpela wrote:
>
>> 12.8.2011 22:01, CqN on cr-48@devChan-13.0.782.43 wrote:
>>> In the following code:
>>>
>>> function t(){
>>>
>>> var d = new Date (string)
>>> var t = d.getTime();
>>>
>>> ...
>>>
>>> delete (d); //IS THIS IMPLCITE by the scope rules of js ???
>>> Essentially redundant?
>>> }

>>
>> It is a no-operation,

>
> No, it is not.


In this case, it is. The state before and after is the same.
Obviosuly, that's assuming that the code isn't strict-mode code.
I think that's a safe assumption in this case.

>> and raises a SyntaxError exception in strict mode.

>
> Which is additional proof that it is _not_ a no-operation.


If "it" refers to the delete operator in general, agree.
If "it" refers to the actual use above, disagree.

>> (On Firefox in non-strict mode, it causes a warning.

>
> _In JavaScript_, not "on Firefox".


Why not "on Firefox" (ok, "in Firefox" would be better, but let's not
be pedantic[1]).

It is Firefox that decides to show a warning in the console.
The Mozilla JavaScript specification has no notion of warnings. See, e.g.,
https://developer.mozilla.org/en/Jav...Special/delete
It's quite possible to have a JavaScript implementation that doesn't
show a warning.

>> The delete operator deletes a property of an object. When the operand is
>> an unqualified variable, as here, it has no effect.

>
> (What do you consider to be a qualified variable, then?)
>
> The `delete' operator deletes properties of native objects that do not have
> the `[[DontDelete]]' attribute (in ES5, properties that do have the
> [[Configurable]] attribute). Identifiers that are declared as variables are
> properties of the Variable Object of the execution context they are declard
> in and have the `DontDelete' attribute (in ES5, they are References to an
> Environment Record binding, respectively).


... unless the variable was declared in eval code, in which case it can
be deleted.

[strict mode is different]

> *Therefore*, and not for other reasons, the `delete' operation *fails* here.


> It is _not_ a no-operation.


The delete operator in general, no. In this case, yes.
It does return false, but the result is ignored, so an optimizing compiler
could get away with generating absolutely no code for the statement.
That's a pretty good sign of being a no-op.

/L
[1] A-ha-ha-haaa. Right. Good one.
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-13-2011
Lasse Reichstein Nielsen wrote:

> Thomas 'PointedEars' Lahn <> writes:
>> Jukka K. Korpela wrote:
>>> 12.8.2011 22:01, CqN on cr-48@devChan-13.0.782.43 wrote:
>>>> In the following code:
>>>>
>>>> function t(){
>>>>
>>>> var d = new Date (string)
>>>> var t = d.getTime();
>>>>
>>>> ...
>>>>
>>>> delete (d); //IS THIS IMPLCITE by the scope rules of js ???
>>>> Essentially redundant?
>>>> }
>>>
>>> It is a no-operation,

>>
>> No, it is not.

>
> In this case, it is. The state before and after is the same.


That is not the definition of a no-operation.

> Obviosuly, that's assuming that the code isn't strict-mode code.


A `delete' operation checks its operand and can throw an exception even when
not in strict mode. A no-operation on the other hand does nothing at all.

>>> and raises a SyntaxError exception in strict mode.

>> Which is additional proof that it is _not_ a no-operation.

>
> If "it" refers to the delete operator in general, agree.
> If "it" refers to the actual use above, disagree.


Your problem. It takes a non-zero number of steps for the `delete'
operation here. If it was a no-operation, the number of steps were zero.

>>> (On Firefox in non-strict mode, it causes a warning.

>>
>> _In JavaScript_, not "on Firefox".

>
> Why not "on Firefox" (ok, "in Firefox" would be better, but let's not
> be pedantic[1]).
>
> It is Firefox that decides to show a warning in the console.
> The Mozilla JavaScript specification has no notion of warnings.


There is no "Mozilla JavaScript specification" – you made that up. Warnings
are a feature of the SpiderMonkey JavaScript script engine embedded in e.g.
Firefox (and compatible implementations, like Rhino and Narcissus); the
error console of Firefox (among other Firefox extensions, like Firebug, and
Mozilla-based browsers) only employs them.

<https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference>

> See, e.g.,
>

https://developer.mozilla.org/en/Jav...Special/delete
> It's quite possible to have a JavaScript implementation that doesn't
> show a warning.


You are confusing browsers with ECMAScript implementations, JavaScript with
ECMAScript, and JavaScript with other ECMAScript implementations.

>>> The delete operator deletes a property of an object. When the operand is
>>> an unqualified variable, as here, it has no effect.

>>
>> (What do you consider to be a qualified variable, then?)
>>
>> The `delete' operator deletes properties of native objects that do not
>> have the `[[DontDelete]]' attribute (in ES5, properties that do have the
>> [[Configurable]] attribute). Identifiers that are declared as variables
>> [[are properties of the Variable Object of the execution context they are
>> declard in and have the `DontDelete' attribute (in ES5, they are
>> References to an Environment Record binding, respectively).

>
> .. unless the variable was declared in eval code, in which case it can
> be deleted.


Unless the eval code is strict code in which case there is nothing to
delete, but the `delete' operation does not throw an exception and it
evaluates to `true'. Which might be a Chromium 13.0.782.107 V8 (presumably
3.3.10.17) bug; but I am not sure if I understand ES5, section 11.4.1, steps
2 and 3 correctly.

Source code:

eval("'use strict'; var x = 42;");

/* throws ReferenceError when uncommented */
// x;

/* true in said Chromium version, no SyntaxError */
delete x;

/* throws ReferenceError */
x;


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57