Strange Javascript Object Behavior

I followed this javascript code:

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...";
}

      

The screen should display:

-I'm moving
-Starting
-Broom broom...

      

But when I do it ...

-Starting
-I'm moving
-Broom broom...

      

Javascript first prints the "direct" document.write (the one with the string written directly) and then prints the ones that return. Why is javascript doing this?

+3


source to share


4 answers


Looking at:

var motorbike = {
    "Wheel" : move(),
    "Motor" : start()
}; 

      

These methods are not run when you call the property, but when the object is being constructed!

That is why the call document.write

of start

and, therefore, you see the first line of: document.write("-Starting<br/>");

.

Try to run this:



var motorbike = {
    "Wheel" : alert('1')
};

      

You will see a warning immediately!

You probably:

var motorbike = {
    "Wheel" : move,
    "Motor" : start
}; // CREATE MOTORBIKE OBJECT

document.write(motorbike.Wheel()); // MOVE MY MOTORBIKE
document.write(motorbike.Motor()); // START MY MOTORBIKE

      

+3


source


When you write:

var motorbike = {
    "Wheel" : move(),
    "Motor" : start()
};

      



the function is executed start

and -Starting

.

-I'm moving

and -Broom broom...

printed after the function document.write()

is called after the object is declared motorbike

.

0


source


If you want to assign functionality to an object, you only need to assign a name without parentheses:

var motorbike = {
    "Wheel" : move,
    "Motor" : start
};

      

If you say Wheel: move()

, the move is done immediately and the result is assigned to the Wheel property. Therefore, move

they start

are executed during initialization and start recording the beginning. The results (as in the strings returned by the function) are written thereafter when written document.write(motorbike.Wheel);

anddocument.write(motorbike.Motor);

PS If functions are assigned, then after using them you need a parenthesis to call the function: document.write(motorbike.Wheel());

0


source


Javascript runs your code from top to bottom.

So, the function move()

is executed and returns -I'm moving<br/>

, which is set as the value of the Wheel. It is then executed start()

and it immediately writes -Starting<br/>

to document

. After this line

document.write(motorbike.Wheel); // MOVE MY MOTORBIKE
document.write(motorbike.Motor); // START MY MOTORBIKE

      

are executed and add values Wheel

and to the document Moter

.

If yours start()

returns all of the text, it will work as you expect.

function start(){
   return "-Starting<br/>-Broom broom...";
}

      

0


source







All Articles