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.

Hi,

A sequence of characters.

When you call a function, you can pass along some values to it, these values are called arguments or parameters.

Round brackets.

No value.

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.

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.:slight_smile:

Hi,

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

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.

function myfunc(a, b, c) { // a, b, and c are arguments
...
}

myfunc(x,y,z); // x, y, and z are parameters

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?

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.

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.

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).

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 [URL=“http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.4.2”]arguments are the values when you invoke it. [URL=“http://perldoc.perl.org/perlsub.html”]Perl is the same way. [URL=“http://en.wikipedia.org/wiki/Parameter_(computer_science)”]Wikipedia describes it this way as well.

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.

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:

[FONT=Courier New]CallExpression Arguments

function Identifier ( FormalParameterListopt ) { FunctionBody }[/FONT]

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.

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.

Thank you, this makes sense (I hope).

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

$("#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.

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.

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.

Thanks for your help.