From: André Hänsel on
Hi,

I am using ExtJS but this should make no difference. I am wondering
why this:

function init() {
var w = {};
w = new Ext.Window({
items: {
keys:{
key: Ext.EventObject.ENTER,
fn: function(){ this.save(); },
scope: w
},
},
save: function()
{
console.log('save');
}
});
}

is different from this:

function init() {
var test;
var w = new Ext.Window({
items: {
keys:{
key: Ext.EventObject.ENTER,
fn: function(){ this.save(); },
scope: test
},
},
save: function()
{
console.log('save');
}
});
test = w;
}

In the first example the scope of the innermost anonymous function (w)
is the empty object while in the second example it's the Ext.Window
object.

Aren't w and test in the same scope? So the additional assignment
shouldn't be necessary, should it?

Regards,
André
From: Lasse Reichstein Nielsen on
Andr� H�nsel <andre(a)webkr.de> writes:

> I am wondering why this:
>
> function init() {
> var w = {};
> w = new Ext.Window({
> items: {
> keys:{
> key: Ext.EventObject.ENTER,
> fn: function(){ this.save(); },
> scope: w

At this point w references the empty object it was assigned above.

> },
> },
> save: function()
> {
> console.log('save');
> }
> });
> }
>
> is different from this:
>
> function init() {
> var test;
> var w = new Ext.Window({
> items: {
> keys:{
> key: Ext.EventObject.ENTER,
> fn: function(){ this.save(); },
> scope: test

At this point, test has the value "undefined", since it hasn't been
assigned a value yet.

> },
> },
> save: function()
> {
> console.log('save');
> }
> });
> test = w;
> }
>
> In the first example the scope of the innermost anonymous function (w)
> is the empty object while in the second example it's the Ext.Window
> object.

Are you sure. It looks like it should be undefined.
Or maybe Ext.js does picks a default if the value is undefined (it does
matter what Ext.Window does with the arguments).

> Aren't w and test in the same scope?

Yes.

> So the additional assignment shouldn't be necessary, should it?

The last assignment to test is a complete no-op. The test variable
goes out of scope immediately after.

Remember that the values of the object literals are evaluated at
object creation time. Assigning to test after that makes no difference
at all.

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

From: André Hänsel on
On 18 Mai, 23:05, Lasse Reichstein Nielsen <lrn.unr...(a)gmail.com>
wrote:
> Are you sure.

No, unfortunately I'm not. Sorry, when setting up a testcase I found
out, that none of the both ways works. I must have seen something
wrong.

What stays is the question, how I get a reference to the Ext.Window
object into that anonymous function, but I guess that's more of an
ExtJS question.

Regards,
André
From: André Hänsel on
It can be so easy...

function init() {
w = new Ext.Window({
items: {
keys:{
key: Ext.EventObject.ENTER,
fn: function(){ w.save(); },
},
},
save: function()
{
console.log('save');
}
});
}
From: Thomas 'PointedEars' Lahn on
André Hänsel wrote:

> It can be so easy...
>
> function init() {
> w = new Ext.Window({
> items: {
> keys:{
> key: Ext.EventObject.ENTER,
> fn: function(){ w.save(); },
> },
> },
> save: function()
> {
> console.log('save');
> }
> });
> }

You did declare `w', didn't you? And you really should eschew ExtJS, it's
proven junk.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)