promotion image of download ymail app
Promoted

HTML的JavaScript高手請進(20點)

請問可以解釋以下的JavaScript嗎??考試要考,但我不明白它的用法

<SCRIPT language="JavaScript">

function square(x){

var y=x*x

window.alert("Square of "+ x +" "= " +y);

return y; // return function result

}

var answer=square(2); //assign square of 2 to answer

window.alert("Returned result="+answer)

</SCRIPT>

另外,

可否解釋一下return (為什麼有時要放在Body裡面,有時候要放在Head那邊?) 的用法和//的用法

還有,

要如何開始寫JavaScript,我看到題目時,完全不知道要如何著手開始寫JavaScript。

希望高手們幫幫我呀!這次考試很重要!

謝謝謝謝!

更新:

不知道要如何著手開始寫JavaScript,就是說不知道何時要寫function

更新 2:

其實類似 name="comment" 這種JavaScript

是不是一定要加name的??

更新 3:

還有method屬性是什麼??

1 個解答

評分
  • 7 年前
    最愛解答

    請問可以解釋以下的JavaScript嗎??考試要考,但我不明白它的用法

    <SCRIPT language="JavaScript">

    function square(x){

    var y=x*x

    window.alert("Square of "+ x +" "= " +y);

    return y; // return function result

    }

    var answer=square(2); //assign square of 2 to answer

    window.alert("Returned result="+answer)

    </SCRIPT>

    /* the script language were used are javascript

    created a function and the function are called square which takes an argument call x

    variable y is equal to argument of x multiply by argument of x

    then result will be displayed "Square of value(x) = value(y)"

    then return the result(y)

    */

    /*variable "answer" is after the method call "square" with an argument of 2

    then display Returned result="answer"

    另外,

    可否解釋一下return (為什麼有時要放在Body裡面,有時候要放在Head那邊?) 的用法和//的用法

    depends what you want... if the method are only action then nothing are returned so you don't need it to return....

    really it's just convenience and how you like....

    on your example you want return because you want to display the value again... i see it as waste, but sometimes you want it to show user again...XD

    還有,

    要如何開始寫JavaScript,我看到題目時,完全不知道要如何著手開始寫JavaScript。

    that is javascript...from your example... usually javascript are activate with HTML

    script are usually created after body start tag

    希望高手們幫幫我呀!這次考試很重要!

    謝謝謝謝!

    you are welcome hope it helps

    2013-06-17 16:08:06 補充

    不知道要如何著手開始寫JavaScript,就是說不知道何時要寫function

    when you want to do something with result, calculations/want result from user

    2013-06-17 16:12:28 補充

    其實類似 name="comment" 這種JavaScript

    是不是一定要加name的??

    that is just javascript related! it's HTML, i perfer you start learning HTML before JS!

    2013-06-17 23:39:47 補充

    還有method屬性是什麼??

    in JS it doesn't have attribute... just whatever returns...

    • Commenter avatar登入以回覆解答
還有問題嗎?立即提問即可得到解答。