SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Location
    Pabna, Bangladesh
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Some definitions of JavaScript

    What is string?
    What is parameter?
    What is parentheses () ?

    Please see the following codes:

    <html>
    <head>
    <script type="text/javascript">
    function show_prompt()
    {
    var name=prompt("Please enter your name","Harry Potter");
    if (name!=null && name!="")
    {
    document.write("Hello " + name + "! How are you today?");
    }
    }
    </script>
    </head>
    <body>

    <input type="button" onClick="show_prompt()" value="Show a prompt box" />

    </body>
    </html>


    name!=null && name!="" is included here.
    Here what is the meaning of null?
    Here what is the meaning of ?

    Thank you.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by foysalu9 View Post
    What is string?
    A sequence of characters.

    Quote Originally Posted by foysalu9 View Post
    What is parameter?
    When you call a function, you can pass along some values to it, these values are called arguments or parameters.

    Quote Originally Posted by foysalu9 View Post
    What is parentheses () ?
    Round brackets.

    Quote Originally Posted by foysalu9 View Post
    Here what is the meaning of null?
    No value.

    Quote Originally Posted by foysalu9 View Post
    Here what is the meaning of ?
    An empty string.

    What the function is doing is checking to see if the variable name contains a sensible value before writing its output to the screen.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Location
    Pabna, Bangladesh
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo,

    Thank you so much for your response.

    I am happy to say that your answers help me a lot.

    I hope you will help me in future again.

    Thank you again.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Location
    Pabna, Bangladesh
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thank you so much for your help. I hope you will help me future again. Keep well.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    When you call a function, you can pass along some values to it, these values are called arguments or parameters.
    Strictly speaking parameters are the values passed to a function when the function is called and arguments are the placeholders used to define how those values will be used in the function itself.


    Code:
    function myfunc(a, b, c) { // a, b, and c are arguments
    ...
    }
    
    myfunc(x,y,z); // x, y, and z are parameters
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by felgall View Post
    Strictly speaking parameters are the values passed to a function when the function is called and arguments are the placeholders used to define how those values will be used in the function itself.
    Oh wow, I didn't know that. Thank you!

    That actually leads me to a second question: in JavaScript terms, is there a difference between a method and a function, or are these two terms synonymous?

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    That actually leads me to a second question: in JavaScript terms, is there a difference between a method and a function, or are these two terms synonymous?
    Strictly speaking JavaScript doesn't have functions or methods - it only has objects and properties.

    All "functions" that are defined directly are "methods" of the window object (or whichever object they are defined inside of) and all "methods" in JavaScript are in fact objects in their own right since they too can have properties and "methods" defined for them.

    In practical terms it makes sense to refer to any methods where you don't specify the object that it belongs to are effectively working as functions and any where you define them as belonging to a specific object but don't attach any properties or additional objects to it are effectively working as methods. Neither situation is entirely clear cut though - particularly if you start defining "methods" on the Function object and run that against all the "functions" in your page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,268
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    ...parameters are the values...arguments are the placeholders...
    It's actually the other way around.

    Though, distinctions such as this only ever seemed to matter in school. In real life, I most frequently hear people use "parameters" to describe both.
    "First make it work. Then make it better."

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    It's actually the other way around.
    When did they reverse it? I clearly remember being taught that the arguments are the placeholders in the function and that you call a function using parameters some time back in the late 1970s or early 80s. I even have books from back then that refer to them that way around. I'd have to check but I am fairly sure that I also have some more recently published books that refer to them that way around as well.

    I agree with you that in practice the distinction between the two is effectively irrelevant and most people use either name to refer to both without it leading to misunderstandings (although it does appear that such usage has led to the meanings getting reversed at some stage).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,268
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    If ever it was the other way around, then that was before my time.

    In Java, parameters are the variables when you declare a method, and arguments are the values when you invoke it. Perl is the same way. Wikipedia describes it this way as well.
    "First make it work. Then make it better."

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    If ever it was the other way around, then that was before my time.
    It definitely was the other way around when JavaScript was created as the JavaScript construct that you use within functions as a placeholder list to access all the values passed to the function is called arguments[] - if it were the way around that you suggest then it would have been called parameters[]

    So effectively you can say that for JavaScript at least the two terms are the way around that I specified even if some languages have reversed the meanings.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  12. #12
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,268
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    It definitely was the other way around when JavaScript was created as the JavaScript construct that you use within functions as a placeholder list to access all the values passed to the function is called arguments[] - if it were the way around that you suggest then it would have been called parameters[]
    Ish. When you access arguments[], you're accessing the values that were passed in... which are referred to as arguments. For example, if a function is defined with one placeholder but invoked with 10 values, then arguments[] will hold 10 values, which means it represents the values passed in (the arguments) and not the placeholders.

    Also, ECMA-script defines the syntax this way:

    CallExpression Arguments

    function Identifier ( FormalParameterListopt ) { FunctionBody }
    "First make it work. Then make it better."

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by felgall View Post
    Strictly speaking JavaScript doesn't have functions or methods - it only has objects and properties ...
    Thank you for your explination, felgall.
    I've been doing a little more reading on the subject and stumbled upon this StackOverflow discussion of the same question (although not related especially to JavaScript).

    I found it quite interesting reading, if not a little confusing and contradictory in places.

  14. #14
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    I found it quite interesting reading, if not a little confusing and contradictory in places.
    Yes that does have some interesting and sometimes contradictory viewpoints.

    One I thought was interesting was that someone tried to make something out of the fact that C++ doesn't use the term method at all without realising that the term 'member function' is used by that language instead. If anything member function is a more descriptive name than method is.

    The thing that probably makes the biggest difference with respect to JavaScript is that JavaScript doesn't have classes and so properties and objects belong directly to objects rather than being defined on a class.

    With JavaScript the most useful way in which to use the different terms is in how you are using the JavaScript code in comparison to how you'd do the same thing in other languages. If it doesn't matter that the 'method' you have just defined is actually attached to an object then you might as well refer to it as a function. If the object that it is attached to does matter then you may as well refer to it as a method. Of course if you use call or apply then you can use it on a completely different object from the one it belongs to and I don't think I have seen a term for referring to the situation where the object you are running the 'method' on isn't the object it belongs to.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by felgall View Post
    If it doesn't matter that the 'method' you have just defined is actually attached to an object then you might as well refer to it as a function. If the object that it is attached to does matter then you may as well refer to it as a method.
    Thank you, this makes sense (I hope).

    So, if I apply this to a little bit of jQuery:

    Code JavaScript:
    $("#button").click(function(){
      // Do stuff
    });

    Would it be fair to say that .click() is a method, as it is attached to $("#button"), a jQuery object, which in this case does matter.
    Whereas the event handler which is passed in to .click() is a function, as it is anonymous and it doesn't matter what it is attached to.

  16. #16
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    Would it be fair to say that .click() is a method, as it is attached to $("#button"), a jQuery object, which in this case does matter.
    Whereas the event handler which is passed in to .click() is a function, as it is anonymous and it doesn't matter what it is attached to.
    That's how I'd generally describe it - it's the way that makes the most sense. A method where the object matters, a function when the object doesn't matter.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  17. #17
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is my first time hearing when a function is invoked/called the parameters are being passed as values, instead of referring function invocations as arguments being passed. I may have to get some research on this.

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by felgall View Post
    That's how I'd generally describe it - it's the way that makes the most sense. A method where the object matters, a function when the object doesn't matter.
    Thanks for your help.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •