[英]Javascript object strange behavior
I executed this javascript code: 我执行了这个javascript代码:
var motorbike = {
"Wheel" : move(),
"Motor" : start()
}; // CREATE MOTORBIKE OBJECT
document.write(motorbike.Wheel); // MOVE MY MOTORBIKE
document.write(motorbike.Motor); // START MY MOTORBIKE
function move(){
return "-I'm moving<br/>";
}
function start(){
document.write("-Starting<br/>");
return "-Broom broom...";
}
In the screen should appear: 在屏幕上应该出现:
-I'm moving
-Starting
-Broom broom...
But when I execute it... 但是当我执行它时......
-Starting
-I'm moving
-Broom broom...
Javascript prints first the "direct" document.write (that which has a string directly written) and then prints the ones which work with a return. Javascript首先打印“direct”document.write(直接写入字符串),然后打印出返回的那些。 Why javascript does that?
为什么javascript会这样做?
Looking at : 看着 :
var motorbike = {
"Wheel" : move(),
"Motor" : start()
};
Those methods run , not when you call the property but when the object is built ! 这些方法运行,而不是在调用属性时,而是在构建对象时!
This is why the document.write
of start
is invoked and hence you see the string from : document.write("-Starting<br/>");
这就是为什么调用
start
的document.write
,因此你会看到来自的字符串: document.write("-Starting<br/>");
first. 第一。
Try run this : 试试这个:
var motorbike = {
"Wheel" : alert('1')
};
You will immediately see the alert immediately ! 您将立即立即看到警报!
You're probably after : 你可能会追随:
var motorbike = {
"Wheel" : move,
"Motor" : start
}; // CREATE MOTORBIKE OBJECT
document.write(motorbike.Wheel()); // MOVE MY MOTORBIKE
document.write(motorbike.Motor()); // START MY MOTORBIKE
When you write: 当你写:
var motorbike = {
"Wheel" : move(),
"Motor" : start()
};
the start
function is executed, and -Starting
printed. 执行
start
功能,并-Starting
打印。
-I'm moving
and -Broom broom...
are printed after because the document.write()
function is called after the motorbike
object declaration. -I'm moving
因为在motorbike
对象声明之后调用了document.write()
函数,所以-Broom broom...
打印后会-I'm moving
-Broom broom...
If you want to assign the functionality to the object, you have to assign the name only, without the parenthesis: 如果要将功能分配给对象,则必须仅指定名称,而不使用括号:
var motorbike = {
"Wheel" : move,
"Motor" : start
};
If you say Wheel: move()
, move is executed immediately and the result is assigned to the property Wheel. 如果你说
Wheel: move()
,则立即执行move并将结果分配给属性Wheel。 Therefore move
and start
are both executed during initalization, and start writes starting. 因此,在初始化期间执行
move
和start
,并开始写入开始。 The results (as in the strings returned by the function) are written thereafter when writing document.write(motorbike.Wheel);
写完
document.write(motorbike.Wheel);
后,会写入结果 (如函数返回的字符串document.write(motorbike.Wheel);
and document.write(motorbike.Motor);
和
document.write(motorbike.Motor);
PS If the functions are assigned, when using them afterwards, you do need the parenthesis, to call the function : document.write(motorbike.Wheel());
PS如果分配了这些函数,那么在以后使用它们时,你需要使用括号来调用函数:
document.write(motorbike.Wheel());
Javascript executes your code from top to down. Javascript从上到下执行您的代码。
So the function move()
gets executed and returns -I'm moving<br/>
which gets set as value of Wheel. 因此函数
move()
被执行并返回-I'm moving<br/>
被设置为Wheel的值。 Then start()
gets executed and it immediately writes -Starting<br/>
to the document
. 然后执行
start()
并立即将-Starting<br/>
写入document
。 After that the lines 之后的线条
document.write(motorbike.Wheel); // MOVE MY MOTORBIKE
document.write(motorbike.Motor); // START MY MOTORBIKE
are executed and append the value of Wheel
and Moter
to the document. 执行并将
Wheel
和Moter
的值附加到文档中。
If your start()
would return the whole text, it would work as you expect. 如果你的
start()
将返回整个文本,它将按预期工作。
function start(){
return "-Starting<br/>-Broom broom...";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.