简单变量作为属性? [英] simple variables as properties?

查看:82
本文介绍了简单变量作为属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全局范围内的变量


var a1 =''全局变量a1的内容'';


可以是引用(带有一些限制)作为


窗口[''a1'']; //或

window.a1; //甚至

窗口['''''''''''';


因此变量名a1可以动态构建,动态。

是否

有用......至少它很有趣。一个人可以玩自我修改

代码。


如果变量是函数内的局部变量


var f = function(){

var a1 =''f'内的局部变量a1的内容;

this.b1 =''f'内的this.b1的内容;


//如何将变量a1称为某事物的属性? x [''a1'']?

什么是x?

返回a1;

}


我有点(!)困惑:函数是对象,但是

var a1里面f不是属性或是吗?

变量this.b1是实际上是全球性的,至少下面的

变量b1在测试中被覆盖。


var b1 =''全局变量b1 contents'';


所以,问题是:


//如何将变量a1称为某物的属性,如果

var a1在函数f里面? x [''a1'']?什么是x?或者它是不可能的?b $ b不可能?

解决方案

9月2日下午4:18,optimistx写道:


全局范围内的变量


var a1 =''全局变量a1的内容'';


可以作为参考(有一些限制)作为


window [''a1'']; //或

window.a1; //甚至

窗口['''''''''''';


因此变量名a1可以构造在

动态飞行。

这是否有用......至少它很有趣。



这很有用。


可以玩自我修改

代码。


如果变量是函数内的局部变量


var f = function(){

var a1 =''f'内的局部变量a1的内容;

this.b1 =''f'内的this.b1的内容;


/ /如何将变量a1称为

的属性? x [''a1'']?



你不是。


什么是x?



变量属于执行上下文的对象。


返回a1;


}


我有点(!)困惑:函数是对象,但是

var a1里面的f不是属性,不是吗?



它是,但它是执行上下文的变量

对象的属性,不能用javascript直接访问。


变量this.b1实际上是全局的,



取决于你如何调用你的函数(如何一个函数被调用

确定 - this - 的值。


至少

变量b1以下是在测试中被覆盖的。


var b1 =''全局变量b1内容'';


所以,问题是:


//如何将变量a1称为某物的属性,



你不能,也不需要to。


如果var a1在函数f中?



没有任何意义,除了词法,这是真的。


x [''a1' ']?什么是x?或者它是不可能的?b $ b不可能?



变量对象无法访问。如果需要引用属于执行上下文本地对象的属性

,请创建一个,为其属性分配

值并将其引用为该属性/>
对象。


Henry写道:

....


>

变量对象无法访问。如果需要引用属于执行上下文本地对象的属性

,请创建一个,为其属性分配

值并将其引用为该属性/>
对象。



/ *感谢您的解释。为了确保我理解正确,我尝试用我自己的话和一个例子重复
。* /


var a1 =''a1''' ;

var b1 =''在b1之外'';


var f = function(){

var a1 =' 'a1'';

this.b1 =''b1'';

alert(''a1 =''+ a1 +''\ nb1 = ''this.b1);

}


f();

alert((''a1 =''+ a1 +''\ nb1 =''+ b1);

/ *

了解到


1)' '函数是对象''

2)''这是指当前对象''

3)''范围是模糊不清的,但是有全局范围而不是-so-global

范围和

范围取决于你如何达到它''

4)microsoft,jquery,eval属于邪恶的帝国,你要告诉那个

oftern,amen


我会猜到


1)变量this.b1里面不同于外面的var b1(*错误!*)

2)f里面的var a1是某个东西的可访问属性(*错误!*)

3)通过f()调用f,执行范围将是对象f,所有

其局部变量将属于范围,' '本地范围''(*错误?!*)


如果我们选择一个普通的新手用几个小时的javascript学习,那么她/他将如何获得
以一种易于记忆的方式有效地了解和学习这些内容吗?图片?冗长的文字解释对我没有帮助。我理解

的东西,如果我可以向一个7-10岁的孩子解释它,那么孩子

用他/她自己的文字和图片正确地解释了它。一个

集合''你可能认为javascript就像这样(例如

图片),但这是错误的!它就像这样(图片的例子)

可能对学习很有用...


* /


optimistx meinte:


[snip]


3)''范围是模糊不清的,但有全球范围而且不那么全球
范围和

范围取决于你如何达到它''



Nope。 JS具有功能范围。这就是全部。


1)变量this.b1 inside与var b1 outside(* wrong!*)



不会因为在你的情况下这个指的是全局对象。


2)f里面的var a1将是某个东西的可访问属性(*错误!*)



为什么?它只能在匹配范围内访问。这就是实现私人财产的方式。在JS中。


3)通过f()调用f,执行范围将是对象f,所有

其局部变量将属于范围,''本地范围''(*错误?!*)



我认为答案是是。
< blockquote class =post_quotes>
冗长的文字说明对我没有帮助。我理解

的东西,如果我可以向一个7-10岁的孩子解释它,那么孩子

用他/她自己的文字和图片正确地解释了它。



我怀疑没有阅读就行了。而且我不确定幼儿是否对客户端脚本感兴趣...


Gregor

-
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com :::Bildagenturfürdenalpinen Raum


A variable in global scope

var a1 = ''contents of global variable a1'';

can be references (with some limitations) as

window[''a1'']; // or
window.a1; // or even
window[''a''+''1''];

Thus the variable name a1 can be constructed on the fly, dynamically.
Whether
that is useful ... at least it is fun. One could play with selfmodifying
code.

If the variable is local inside a function

var f = function () {
var a1 = ''contents of local variable a1 inside f'';
this.b1 = ''contents of this.b1 inside f'';

// how to refer to variable a1 as a property of something? x[''a1''] ?
what is x?
return a1;
}

I am a bit (!) confused: functions are objects, but
var a1 inside f is not a property or is it?
Variable this.b1 is actually global, at least the
variable b1 below was overwritten in a test.

var b1 = ''global variable b1 contents'';

So, the question is:

// how to refer to variable a1 as a property of something, if
var a1 is inside the function f? x[''a1''] ? what is x? Or is it
impossible?

解决方案

On Sep 2, 4:18 pm, optimistx wrote:

A variable in global scope

var a1 = ''contents of global variable a1'';

can be references (with some limitations) as

window[''a1'']; // or
window.a1; // or even
window[''a''+''1''];

Thus the variable name a1 can be constructed on the
fly, dynamically.
Whether that is useful ... at least it is fun.

It is useful.

One could play with selfmodifying
code.

If the variable is local inside a function

var f = function () {
var a1 = ''contents of local variable a1 inside f'';
this.b1 = ''contents of this.b1 inside f'';

// how to refer to variable a1 as a property of
something? x[''a1''] ?

You don''t.

what is x?

The "Variable" object belonging to the execution context.

return a1;

}

I am a bit (!) confused: functions are objects, but
var a1 inside f is not a property or is it?

It is, but it is a property of the execution context''s Variable
object, which is not directly accessible with javascript.

Variable this.b1 is actually global,

Depending on how you call your function (as how a function is called
determines the value of - this - ).

at least the
variable b1 below was overwritten in a test.

var b1 = ''global variable b1 contents'';

So, the question is:

// how to refer to variable a1 as a property of something,

You cannot, and don''t need to.

if var a1 is inside the function f?

There is no sense in which that is true, except lexically.

x[''a1''] ? what is x? Or is it
impossible?

Variable objects are inaccessible. If you need to reference properties
of an object that is local to an execution context, create one, assign
values to its properties and reference them as properties of that
object.


Henry wrote:
....

>
Variable objects are inaccessible. If you need to reference properties
of an object that is local to an execution context, create one, assign
values to its properties and reference them as properties of that
object.

/*Thanks for your explanation. To be sure that I understood correctly I try
to repeat with my own words and an example.*/

var a1 =''outside a1'';
var b1 =''outside b1'';

var f = function () {
var a1 =''inside a1'';
this.b1 = ''inside b1'';
alert(''a1 = '' + a1 + ''\nb1 = '' + this.b1);
}

f();
alert((''a1 = '' + a1 + ''\nb1 = '' + b1);
/*
Having learnt that

1) ''functions are objects''
2) ''this refers to the current object''
3) ''scope is something obscure, but there is global scope and not-so-global
scopes and
scope depends on how you come to it''
4) microsoft, jquery, eval belong to the evil empire and you shoul tell that
oftern, amen

I would have guessed that

1) variable this.b1 inside is different from var b1 outside (* wrong!*)
2) var a1 inside f would be an accessible property of something (*wrong!*)
3) having called f by f() the scope of execution would be the object f, all
its local variables would belong to the scope, ''local scope'' (*wrong?!*)

If we pick an average newbie with some hours of javascript learning, how
would she/he know and learn these effectively and in an easy-to-remember
way? Pictures? Lengthy text explanations do not help me. I understand
something, if I can explain it to a 7-10 year old child so that the child
explains it to me correctly with his/her own words and pictures. A
collection ''you might think javascript works like this (example with
pictures ), but that is wrong!, it works like this (example with pictures)
might be useful for learning...

*/


optimistx meinte:

[snip]

3) ''scope is something obscure, but there is global scope and not-so-global
scopes and
scope depends on how you come to it''

Nope. JS has function scope. That''s all.

1) variable this.b1 inside is different from var b1 outside (* wrong!*)

No. Because in your case "this" refers to the global object.

2) var a1 inside f would be an accessible property of something (*wrong!*)

Why? It''s only accessible within the matching scope. That''s the way to
implement private "properties" in JS.

3) having called f by f() the scope of execution would be the object f, all
its local variables would belong to the scope, ''local scope'' (*wrong?!*)

I suppose the answer is "yes".

Lengthy text explanations do not help me. I understand
something, if I can explain it to a 7-10 year old child so that the child
explains it to me correctly with his/her own words and pictures.

I doubt it will go without reading. And I''m not sure whether toddlers
are interested in client side scripting...

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum


这篇关于简单变量作为属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆