The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Of course there's an easy way, google and see what parameters it accepts When you do document.write you tell it hey, write this argument to the document. When you use myArray.sort() you tell it hey, organize this array alphabetically. The optional parameter it accepts is a compare function sort([comparefunction]) which is basically a function you create that returns a certain way to sort. When you do sort(myArray) it's telling it hey, sort absolutely nothing and take this function myArray as an argument. Of course myArray is an array not a function and you're trying to sort nothing so it will return an error.

except for the type of parameter that they expect. One expects a text string and the other expects a function.

The number and type of parameters that the different methods use are different for each method and depend on what the method is doing.

With those that are supplied as part of JavaScript you just need to look up the parameters required if you don't know what they are. For those that you define for yourself you will know what parameters are required because you decided that when you wrote it.

I don't think I'll remember which methods take which parameters by reading a list.

Eh, it's just like anything else you get into. Take for example if you're a big basketball fan. When you watch you learn positions, players, teams, strategy, rules, stats etc. Without even thinking twice about it you can rattle on about whos playing who what when where why, all these technical terms, players; someone who hasn't heard of basketball would think you're speaking in Chinese. Ever listen to a little kid talk about Pokemon, omg what are they talking about lol. It's not that it's impossible to learn, it's just that you haven't taken the time to learn it programming is no different from the next thing.

You can sort on different criteria: ascending, descending, numerical, lexicographically, etc. Thus, the array foo may be sorted in one particular way, bar another.

This is where the terminology gets a bit confusing. Terms like "method" and "function" get used interchangeably across different languages. In the case of JavaScript's sort(), we can write our own functions to alter the way in which it sorts.

If you call foo.hello(), the first function will be executed, while if you call bar.hello(), the second function will be called. It's two different functions, but they are called the same way. Because of this, the code which uses an object, does not need to know exactly how it works. That is a very powerful concept, which helps to structure your code.

If you call foo.hello(), the first function will be executed, while if you call bar.hello(), the second function will be called. It's two different functions, but they are called the same way. Because of this, the code which uses an object, does not need to know exactly how it works. That is a very powerful concept, which helps to structure your code.

Powerful indeed. Though, I'm a bit confused what creating methods with anonymous functions for custom objects has to do with calling the native sort() function? By the way, it may get confusing setting up custom methods with the same name that do different things.

Because you can change the way sort() works by the same mechanism. If I remember correctly, the Array.sort() function sorts lexicographically, so if you have an array of numbers, they'll come out like "1, 10, 100, 2, 3, 30, 4, ..." which isn't what you want. But if you did something like:

We were talking about the sort function in itself, not what it returns. He is claiming by passing different objects to sort it may change the core functionality of the method thus pointing to different functions entirely. Though, I highly doubt the sort method is going to make sense of an arbitrary variable that the user has created himself.

Yeah. I was trying to make a concrete example of an abstract principle. In retrospect, I can see how that could be misunderstood. I certainly wouldn't recommend changing built-in functions, such as Array.sort().

Because javascript is an imperative programming language, where HTML is a declarative language. HTML works on a higher abstraction level, than javascript does, which is why it's easier to comprehend. With javascript, you're taken more at face value; If you ask for something, then it will happen exactly that way - it won't be interpreted first.

There are plenty attempts to build abstractions within javascript, which - when they are done right - makes the language easier to work with. These are called libraries, or frameworks. You could have a look on some of them; I'm guessing that jQuery might suit you. It's not easy to build good abstractions though - HTML has been developing through a long time, to reach the level it has today, and it's still not perfect.

After a few years of relying on cut and paste, I have decided to learn JavaScript properly.

The first question I have (of many) is this: is there a hard and fast rule that makes it easy to remember which methods take arguments inside the brackets like

Code:

document.write(argument);

as opposed to the ones you stick after the variable and a dot like

Code:

myArray.sort();

Why is that not:

Code:

sort(myArray);

???

Everything in Javascript is essentially an object. Many of the functions that are built into Javascript are actually methods of an object.

For instance arrays in Javascript are objects. They have certain functions attached to them that you can use to work with their contents. If that method doesn't need any more information to do it's job, it won't require an argument.

In the case of sorting, Javascript uses a specific algorithm to sort the contents of an array. Because it is a method of an array and not a standalone function, it already has all the information it needs to act.

Code:

var myarray = new Array('A','C','B','E','D');
myarray.sort();

Because sort() is an array method, the data that the array contains is there in the object for it to act upon.

If sort was just a function, not connected to an object, it would need for you to tell it what to sort.

A string is also an object in Javascript. All strings have methods that can be used to work with it's contents. If you want to split a string, the split method of the string only needs one piece of information from you, and that is the character to split the string on.

Code:

var mystring = 'one,two';
var myarray = mystring.split(',');

That code tells the string object, to take it's contents, split it into pieces everywhere it finds a comma and return the pieces in the form of an array.

In the example above, you then have an array that has the string "one" in one element and the string "two" in the second element.

There are plenty of Javascript references both online and in print to use for documentation on this. Every language has functions or methods that take a varying amount of arguments. The documentation will guide you.