SitePoint Sponsor |
|
User Tag List
Results 1 to 23 of 23
-
Sep 12, 2007, 14:54 #1
When do you stick stuff in the brackets????
Hi folks,
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);
Code:myArray.sort();
Code:sort(myArray);
-
Sep 12, 2007, 15:55 #2
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.
-
Sep 12, 2007, 16:02 #3
It looks like learning by experience is the way past this one. I don't think I'll remember which methods take which parameters by reading a list. Thanks (again) bals28mjk
Last edited by palgrave; Sep 12, 2007 at 16:03. Reason: typo
-
Sep 12, 2007, 17:58 #4
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- Mentioned
- 25 Post(s)
- Tagged
- 1 Thread(s)
There is no real difference between
document.write(contenttobewritten)
and
myarray.sort(nameofcomparefunction)
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.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="^$">
-
Sep 12, 2007, 18:34 #5I don't think I'll remember which methods take which parameters by reading a list.
programming is no different from the next thing.
-
Sep 12, 2007, 22:57 #6
- Join Date
- Aug 2006
- Location
- Samsara
- Posts
- 451
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
http://w3schools.com/jsref/default.asp
Read up on the various JavaScript objects (Math, String, Array, Date, etc.) and just get a feel for each one of them. Each object has a number of functions assigned to it.
It's really the only way to wrap your head around it.
Cheers,
D.
-
Sep 13, 2007, 00:44 #7
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In myArray.sort() the sort function is a member of myArray. Thus foo.sort() may be a different function entirely from bar.sort().
-
Sep 13, 2007, 01:07 #8Thus foo.sort() may be a different function entirely from bar.sort().
-
Sep 13, 2007, 01:36 #9
- Join Date
- Aug 2006
- Location
- Samsara
- Posts
- 451
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
Cheers,
D.
-
Sep 13, 2007, 03:55 #10
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Assume the following:
Code:var foo = {}; foo.hello = function() { alert("hello from foo"); } var bar = {}; bar.hello = function() { alert("hello from bar"); }
-
Sep 13, 2007, 05:21 #11
-
Sep 13, 2007, 10:51 #12
Originally Posted by kyberfabrikken
-
Sep 13, 2007, 18:47 #13
- Join Date
- Aug 2006
- Location
- Samsara
- Posts
- 451
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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:
Code:function num( a, b ){ return a - b; } alert( myarray.sort( num ) );
Cheers,
D.
-
Sep 13, 2007, 20:47 #14
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.
-
Sep 14, 2007, 01:02 #15
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Sep 14, 2007, 01:42 #16
Originally Posted by kyberfabrikken
Originally Posted by kyberfabrikken
Originally Posted by kyberfabrikken
-
Sep 14, 2007, 05:34 #17
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Sep 14, 2007, 15:21 #18
-
Sep 14, 2007, 16:01 #19
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
syntax
Originally Posted by palgrave
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Sep 15, 2007, 11:40 #20
Singers have difficult third albums, I have a difficult third layer of the web.
-
Sep 15, 2007, 12:31 #21
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Sep 17, 2007, 19:54 #22
- Join Date
- Nov 2004
- Location
- Lincoln Nebraska
- Posts
- 1,161
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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();
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(',');
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.
-
Sep 17, 2007, 20:13 #23
Very well said hammer. I wish I knew that when I first started, understanding those concepts makes a world of difference
.
Bookmarks