SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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);
    as opposed to the ones you stick after the variable and a dot like

    Code:
    myArray.sort();
    Why is that not:

    Code:
    sort(myArray);
    ???

  2. #2
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,834
    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="^$">

  5. #5
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePοint Troll disgracian's Avatar
    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.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    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().

  8. #8
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thus foo.sort() may be a different function entirely from bar.sort().
    What do you mean exactly by different function?

  9. #9
    SitePοint Troll disgracian's Avatar
    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.

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bals28mjk View Post
    What do you mean exactly by different function?
    Assume the following:
    Code:
    var foo = {};
    foo.hello = function() {
      alert("hello from foo");
    }
    
    var bar = {};
    bar.hello = function() {
      alert("hello from bar");
    }
    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.

  11. #11
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    For those that you define for yourself you will know what parameters are required because you decided that when you wrote it.
    em, not quite yet!

    Back to my original question, I didn't mean to state the obvious when saying I wasn't going to learn it from a list. I just thought there might be an easy, systematic way of remembering these things.

    Why can't it all be like html

  12. #12
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    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.

  13. #13
    SitePοint Troll disgracian's Avatar
    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 ) );
    It would return the numbers in their proper order: "1, 2, 3, 4, 10, 30, 100, ..."

    Cheers,
    D.

  14. #14
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  15. #15
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bals28mjk View Post
    ... I'm a bit confused what creating methods with anonymous functions for custom objects has to do with calling the native sort() function? ...

    I weren't referring in particular to the sort() function of Array, which you probably don't want to change. It was just meant as a general statement.

    Array.sort() is a bit special, in that it takes a callback (a function) as argument.

  16. #16
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Array.sort() is a bit special, in that it takes a callback (a function) as argument.
    I cringed when palgrave used this method in his question since it is, infact a bit special.
    Quote Originally Posted by kyberfabrikken
    I weren't referring in particular to the sort() function of Array, which you probably don't want to change. It was just meant as a general statement.
    Sounded fairly specific:
    Quote Originally Posted by kyberfabrikken
    In myArray.sort() the sort function is a member of myArray. Thus foo.sort() may be a different function entirely from bar.sort().
    Though, I'm glad the confusion is cleared up and seems that we're on the same page now.

  17. #17
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bals28mjk View Post
    Sounded fairly specific:
    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().

  18. #18
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  19. #19
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,189
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    syntax

    Quote Originally Posted by palgrave
    Why can't it all be like html
    Judging by the number of pages with invalid mark-up, I'd say learning HTML isn't all that easy either. Just that browsers are better at dealing with mistakes than javascript interpreters are.

  20. #20
    SitePoint Addict palgrave's Avatar
    Join Date
    Jan 2006
    Posts
    357
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Singers have difficult third albums, I have a difficult third layer of the web.

  21. #21
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by palgrave View Post
    Why can't it all be like html
    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.

  22. #22
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by palgrave View Post
    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);
    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.

  23. #23
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very well said hammer. I wish I knew that when I first started, understanding those concepts makes a world of difference .


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
  •