困惑 [英] Confused

查看:56
本文介绍了困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:


我用OOP和C ++编程超过10年但我是新的

到javascript。我对一个方面感到非常困惑,如果有人能够解释以下2个&n>
组的陈述之间的差异,我会很感激




函数myFunc(x)

{

a = 1;

var a = 1;

a:1;

this.a = 1;


b = x;

var b = x;

b:x;

this.b = x;


}

TIA

Marvin

解决方案



" Marvin" <毫安********** @ sdc-dsc.gc.ca>在留言中写道

news:11 ********************** @ u72g2000cwu.googlegr oups.com ...

嗨:

我用OOP和C ++编程超过10年但是我是javascript的新手。我对一个方面感到非常困惑,如果有人能够解释以下两组语句之间的差异,我会很感激:

函数myFunc(x)
{
a = 1;
JS预扫描函数查找本地var定义,因此下一个语句中的'var a''

会在前面的
statment。

因此前面的陈述将值1分配给局部变量a。 var a = 1;
将值1赋给局部变量''a''a:1;
''a''是一个标签,可以是中断或继续声明的目标。

标签后面的语句评估为''1'。即它什么也没做。 this.a = 1;
取决于如何调用myFunc。如果它被调用为myVal = myFunc(arg),

this.a指的是包含myFunc的范围中的var a,通常是全局

范围。所以,this.a将引用全局变量'a''。如果调用myFunc

喜欢MyObj = new myFunc(arg),this.a将引用myFunc的属性,

可以引用为myObj.a。在任何属于

myFunc的原型属性的函数中,它可以作为this.a引用。
下面的片段与上面的片段做了相同的事情,除了

赋值是针对参数''x'的本地副本。如果是一个JS

原子数据类型(布尔值,数字,空,未定义),则x的值为

分配。对于字符串和其他对象,分配了对呼叫者x的引用

。 b = x;
var b = x;
b:x;
this.b = x;

}

TIA
Marvin




希望这会有所帮助。

Vic




Vic Sowers写道:

< snip>

this.a = 1;取决于如何调用myFunc。如果它被调用为myVal = myFunc(arg),那么this.a引用包含myFunc的范围中的var a,通常是全局
范围。所以,this.a将引用全局变量'a''。如果调用myFunc像MyObj = new myFunc(arg),this.a将引用myFunc的属性,
可以引用为myObj.a。




嗯?????在任何属于
myFunc'原型属性的函数中,它可以被引用为this.a。



< snip>


对不起,我应该把我的问题写成如下.....

以下是什么区别:


myFunc (x){a = 1; }

myFunc(x){var a = 1; }

myFunc(x){this.a = 1; }

myFunc(x){a:1; }


我理解最后一个。在第1节,我认为a是指a。定义为

一个局部变量。在第二个中,a表示a。是一个全局变量。但我仍然对this.a感到困惑。我特别感到困惑的是

之间的差异:


x = myFunc(arg);

x = new myFunc(arg);

x = myFunc;


我认为第一个只是执行 myFunc并返回一个值。什么

关于另外2个?

另外,是这样的:


o = {a:1,b:2,c :3};


与以下相同?


o = new Object;

oa = 1;

ob = 2;

oc = 3;

TIA

Marvin


Marvin写道:

Vic Sowers写道:
< snip>

> this.a = 1;取决于如何调用myFunc。如果它被调用为myVal =
myFunc(arg),则this.a引用包含
myFunc的范围中的var a,通常是全局范围。所以,this.a将引用
全局变量''a''。如果myFunc被调用为MyObj = new myFunc(arg),
this.a将引用myFunc的属性,可以作为myObj.a引用



嗯?????




他的意思是


var myObj = ...


或引用为MyObj.a。


BTW,您的问号键是borken。

< blockquote class =post_quotes>在任何属于
myFunc的原型属性的函数中,它可以被引用为this.a。


< snip>

抱歉,我的问题应该如下......
以下是什么区别:

myFunc(x){a = 1; }
myFunc(x){var a = 1; }
myFunc(x){this.a = 1; }
myFunc(x){a:1; }




区别在于不同的代码。这些行的共同之处在于,没有一个语法是正确的,因为缺少`function''关键字。

我理解最后一个。


你确定吗?

在第一天,我认为是a被定义为局部变量。


(为简洁起见,我们假设以下每条上面的

行以function为前缀。)


不,不是。因为在那个

执行上下文中没有声明变量'a'',所以你要向范围链中的下一个对象添加一个新属性

,通常是全局对象(但是已经知道

与主机对象的副作用,例如在IE中)。这与定义全局变量类似。


这里的行为与

代码的行为之间的区别在您之前的帖子中,在同一个执行上下文中有一个变量声明

表示a。因此,第一个语句

确实引用了范围链中具有这样一个属性的下一个对象,

这是当前执行上下文的VariableObject(并且

因此`a''引用局部变量。即使

VariableStatement跟随赋值,它首先被解析;

初始化,但是,在不合格的

分配之后发生了。)


应该总是声明变量,或者它们没有附加变量

to一个执行上下文,但是作用域链中任何对象的属性。

在第二个中,a是一个全局变量。


不,不是。因为使用了`var''关键字(一个VariableStatement),

创建了当前执行上下文的变量对象的新属性

。因此,声明了一个局部变量。在此执行上下文中引用

`a''引用该变量,而不是之前可能已创建的

(全局)属性(通过调用第一个

版myFunc())。

但我仍然对this.a感到困惑。我特别感到困惑的是:

x = myFunc(arg);


如果'arg''存在(我将从现在开始假设为了简洁),如果像这样调用
myFunc(),则调用它作为全局对象的方法

(除非在全局

对象之前的作用域链中有另一个对象,见上文)。因此,myFunc()中的this指的是

对象。

x = new myFunc(arg);


如果myFunc()用作这样的NewExpression中调用的构造函数,那么在myFunc()中的
`the''指的是是建造的。 (那个

对象直接从myFunc.prototype继承,因此称为

" myFunc对象。)

x = myFunc;


这不会调用myFunc(),因为它不是CallExpression(缺少

参数列表)或NewExpression(没有前缀`新的''

关键字)。为`x''赋值myFunc,它是对

函数对象的引用。因此,在该行之后,x()可以是[[Call]] ed和

[[Construct]],就好像myFunc()是[[Call]] ed或[[Construct]]编辑。它是

没有复制一个对象,它正在复制对该对象的引用。

[...]
另外,是这样的:

o = {a:1,b:2,c:3};

与以下相同?

o = new Object;
oa = 1;
ob = 2;
oc = 3;




它是。 Object literal既是构造函数又是初始值函数,

因此是一个数组文字[1,2,3]和一个RegExp文字/ 1 + 2 * 3 /。与RegExp文字的
差异在于,在执行之前创建了相应的RegExp

对象,因此只创建一次,即使是

if它在代码中被多次使用。


这是非常基本的知识(无论用什么术语来形容

)。请搜索档案,RTFAQ [1] RTFM和STFW,然后再次发布



HTH


PointedEars

___________

[1]< URL:http://jibbering.com/faq/>


Hi:

I have been programming with OOP and C++ for over 10 years but I am new
to javascript. I am very confused about one aspect and would appreciate
it if someone could explain the differences between the following 2
groups of "statements":

function myFunc (x)
{
a = 1;
var a = 1;
a: 1;
this.a = 1;

b = x;
var b = x;
b: x;
this.b = x;

}
TIA
Marvin

解决方案


"Marvin" <ma**********@sdc-dsc.gc.ca> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Hi:

I have been programming with OOP and C++ for over 10 years but I am new
to javascript. I am very confused about one aspect and would appreciate
it if someone could explain the differences between the following 2
groups of "statements":

function myFunc (x)
{
a = 1; JS pre-scans the function looking for local var definitions so the ''var a''
in the next statement creates a local var for ''a'' in the preceeding
statment.
So the preceding statment assigns the value ''1'' to the local var ''a''. var a = 1; Assigns the value 1 to the local var ''a'' a: 1; ''a'' is a lable, which can be the target of a break or continue statement.
The statement that follows the lable evaluates to ''1''. i.e. it does nothing. this.a = 1; Depends on how myFunc is called. If it is called like myVal = myFunc(arg),
this.a refers to the var a in the scope enclosing myFunc, usually the global
scope. So, this.a would refer to the global var ''a''. If myFunc is called
like MyObj = new myFunc(arg), this.a will refer to a property of myFunc and
can be referenced as myObj.a. In any functions which are a property of
myFunc''s prototype property, it can be referenced as this.a. The fragment below does the same things as the fragment above except the
assignments are for the local copy of the argument ''x''. if the is a JS
atomic data type (Boolean, Number, null, undefined), the value of x is
assigned. For Strings and other Objects, a reference to the callers ''x'' is
assigned. b = x;
var b = x;
b: x;
this.b = x;

}
TIA
Marvin



Hope this helps.
Vic



Vic Sowers wrote:
<snip>

this.a = 1; Depends on how myFunc is called. If it is called like myVal = myFunc(arg),
this.a refers to the var a in the scope enclosing myFunc, usually the global
scope. So, this.a would refer to the global var ''a''. If myFunc is called
like MyObj = new myFunc(arg), this.a will refer to a property of myFunc and
can be referenced as myObj.a.



Huh????? In any functions which are a property of
myFunc''s prototype property, it can be referenced as this.a.


<snip>

Sorry, I should have phrased my question as follows.....
What is the difference between the following:

myFunc(x) { a = 1; }
myFunc(x) { var a = 1; }
myFunc(x) { this.a = 1; }
myFunc(x) { a : 1; }

I understand the last one. In the 1st, I take it that "a" is defined as
a local variable. In the 2nd, "a" is a global variable. But I am still
confused about "this.a". I am especially confused about the differences
between:

x = myFunc(arg);
x = new myFunc(arg);
x = myFunc;

I think that the first just "executes" myFunc and returns a value. What
about the other 2?
Also, is this:

o = {a:1, b:2, c:3};

the same as the following?

o = new Object;
o.a =1;
o.b = 2;
o.c = 3;
TIA
Marvin


Marvin wrote:

Vic Sowers wrote:
<snip>

> this.a = 1; Depends on how myFunc is called. If it is called like myVal =
myFunc(arg), this.a refers to the var a in the scope enclosing
myFunc, usually the global scope. So, this.a would refer to the
global var ''a''. If myFunc is called like MyObj = new myFunc(arg),
this.a will refer to a property of myFunc and can be referenced
as myObj.a.



Huh?????



He meant

var myObj = ...

or "referenced as MyObj.a".

BTW, your Question Mark key is borken.

In any functions which are a property of
myFunc''s prototype property, it can be referenced as this.a.


<snip>

Sorry, I should have phrased my question as follows.....
What is the difference between the following:

myFunc(x) { a = 1; }
myFunc(x) { var a = 1; }
myFunc(x) { this.a = 1; }
myFunc(x) { a : 1; }



The difference is different code. The lines have in common that neither
one is syntactically correct, since the `function'' keyword is missing.
I understand the last one.
Are you sure?
In the 1st, I take it that "a" is defined as a local variable.
(Let us assume in the following for brevity that each of the above
lines is prefixed with `function ''.)

No, it is not. Since in there is no variable `a'' declared in that
execution context, you are adding a new property to the next object
in the scope chain, usually the Global Object (but there are known
side effects with host objects, e.g. in IE). This would be similar
to defining a global variable.

The difference between the behavior here and the behavior with the
code in your previous posting is that there was a VariableDeclaration
for `a'' within the same execution context. Hence the first statement
did refer to the next object in the scope chain that had such a property,
which was the VariableObject of the current execution context (and
therefore `a'' referred to the local variable. Even though the
VariableStatement followed the assignment, it was parsed first;
the initialization, however, happened later, after the unqualified
assignment.)

Variables should always be declared, or they are no variables attached
to an execution context, but properties of any object in the scope chain.
In the 2nd, "a" is a global variable.
No, it is not. Because the `var'' keyword (a VariableStatement) was used,
a new property of the Variable Object of the current execution context
was created. Therefore, a local variable was declared. References to
`a'' within this execution context refer to that variable, not to the
(global) property that may have been created before (by calling the first
version of myFunc()).
But I am still confused about "this.a". I am especially confused about the
differences between:

x = myFunc(arg);
Provided that `arg'' exists (which I will assume from now for brevity), if
myFunc() is called like this, it is called as a method of the Global Object
(unless there is another object in the scope chain before the Global
Object, see above). Therefore, `this'' within myFunc() refers to that
object.
x = new myFunc(arg);
If myFunc() is used as a constructor as called in a NewExpression like here,
`this'' within myFunc() refers to the object that is constructed. (That
object inherits directly from myFunc.prototype, and is therefore called a
"myFunc object".)
x = myFunc;
This does not call myFunc(), because it is not a CallExpression (the
argument list is missing) or a NewExpression (there is no prefixed `new''
keyword). `x'' is assigned the value of myFunc, which is a reference to a
Function object. Therefore, after that line, x() can be [[Call]]ed and
[[Construct]]ed as if myFunc() was [[Call]]ed or [[Construct]]ed. It is
is not copying an object, it is copying the reference to that object.
[...]
Also, is this:

o = {a:1, b:2, c:3};

the same as the following?

o = new Object;
o.a =1;
o.b = 2;
o.c = 3;



It is. An Object literal is both a constructor and an initializer,
so is an Array literal [1, 2, 3], and a RegExp literal /1+2*3/. The
difference with the RegExp literal is that the corresponding RegExp
object is created before execution, and so is only created once, even
if it is used several times in the code.

This is very basic knowledge (no matter the terms used to describe
it). Please search the archives, RTFAQ[1] RTFM, and STFW, before
you post again.
HTH

PointedEars
___________
[1] <URL:http://jibbering.com/faq/>


这篇关于困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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