Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Why are methods called "methods?"

    They're functions. Why call them "methods?" If there has to be a special name for them, why not "object functions" or "property functions" or "private functions" or even "subfunctions" or something? Calling them something completely different is confusing.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    When you combine an object ( HTML element ) with a method ( onclick ? ) you can use that to call a function. That seems perfectly clear to me, and good syntax, since the parts are functionally distinct but related.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    It is usual with procedural languages to refer to variables and functions.

    With object oriented languages you have objects that can have properties and methods belonging to those objects.

    It is the association of a method with an object rather than beins stand alone that makes it a method rather than a function.

    JavaScript is slightly more complicated because it is a prototyping language rather than either procedural or object oriented.

    In JavaScript all variables are properties because they all have to belong to an object. If you don't specify the object then they get attached to an existing object - usually either the window or global object depending on where the script is running.

    Also in JavaScript all functions are methods because they all belong to and object. The following belong to the global object and since that object cannot be referenced directly are the only ones that might be considered to be functions - parseInt, parseFloat, isNaN, isFinite, encodeURL, encodeURLComponent, decodeURL, decodeURLComponent, setTimeout, setInterval.

    In JavaScript all functions/methods are also objects because they can have methods attached to them.

    The one JavaScript object that is a special case is Date because browsers normally implement it in a way that does not permit normal inheritance for creation of your own custom objects.

    JavaScript objects should not be confused with HTML elements - the HTML elements are generally referred to as nodes from within JavaScript. A node is a type of JavaScript object that corresponds to an HTML element. Methods are usually attached to nodes via an event listener that defines the event that needs to occur on the node in order for the method to be run.

    It doesn't really matter whether you refer to variables or properties - as far as JavaScript is concerned they are basically the same thing. Similarly it doesn't matter whether you refer to functions, methods or objects as they are all basically the same thing in JavaScript.
    Last edited by felgall; 10-23-2013 at 02:50 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,176
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Quote Originally Posted by SomethingFunny View Post
    Calling them something completely different is confusing.
    "And Ramirez hits the ball with his bat, such that it lands in the outfield between the left field player and the center field player and such that Ramirez is able to run and reach second base without being tagged."

    compared to

    "Ramirez hits a double into the left side gap."

    *********

    Pardon me, but it is *LESS* confusing to have concise "jargon" that is well understood by all who follow a given activity, be it baseball or programming. In point of fact, I have argued for the last 30 years that programming has TOO LITTLE jargon. We constantly find ourselves using the same words to mean different things because we don't have enough words to cover all the things we need to cover. Consider the word "system" and how many different things it can mean. Compare programming to medicine, where doctors have specific words for almost everything in the human body and in ever condition of the human body. Programming comes off as the poor second cousin.

    So I completely disagree with your position.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Old Pedant View Post
    I have argued for the last 30 years that programming has TOO LITTLE jargon. We constantly find ourselves using the same words to mean different things because we don't have enough words to cover all the things we need to cover. Consider the word "system" and how many different things it can mean. Compare programming to medicine, where doctors have specific words for almost everything in the human body and in ever condition of the human body. Programming comes off as the poor second cousin.
    The comparison I usually use is between programming and the building industry. The building industry has lots of different terms for the different types of jobs - brick layer, plumber, carpenter, roofer etc. Programming has hundreds of times as many different jobs as the building industry does but only a small fraction of the number of job titles. That's just for the different jobs involved in programming - when it comes to the different things that the people in all those different roles need to deal with the same term in many cases gets used to mean several different things for each of those different jobs with only an occassional overlap in meaning between jobs.

    Some companies have even introduced their own "jargon" to ensure that everyone knows exactly what is being referred to from the numerous things that go by the same name in more common usage. For example IBM used lots of TLA (three letter acronyms) in their internal jargon (I don't know if they still do) with AMD (air movement device) being the jargon used to refer to a particular type of fan.


    Programming definitely contains far too few words to be able to accurately describe everything. We need a couple of million extra words so that we can be sure that everyone knows exactly what is being referred to.
    Last edited by felgall; 10-23-2013 at 10:01 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Quote Originally Posted by SomethingFunny View Post
    They're functions. Why call them "methods?" Calling them something completely different is confusing.
    it's not that methods are functions are different, methods are functions.

    all robots are computers but not all computers are robots.

    the word method implies ownership and in javascript's case, ownership affects the execution of code inside the function.

    in the same notion, Arrays are Objects, so why even have an Array constructor? because it's useful to coders to distinguish certain particulars about the operation of the structure.
    Last edited by rnd me; 10-23-2013 at 11:33 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Quote Originally Posted by felgall View Post
    In JavaScript all variables are properties because they all have to belong to an object. If you don't specify the object then they get attached to an existing object - usually either the window or global object depending on where the script is running.

    Also in JavaScript all functions are methods because they all belong to and object.
    what object does the variable "b" belong to in the below code? or the variable "c", which you claim is a method?

    Code:
    function a(){
     var b=5;
     function c(){}
     return b;
    }
    confused here...



    BTW, Window.prototype.setTimeout defines setTimeout, and many of the other "functions" that you listed, technically making them methods of window.prototype, not pure functions. in reality, some can be re-bound and others expect this===window.

    the following are all defined that way:

    in chrome, Object.getOwnPropertyNames(Window.prototype) ==
    toString,postMessage,close,blur,focus,ondeviceorientation,ondevicemotion,ontransitionend,onwebkittra nsitionend,onwebkitanimationstart,onwebkitanimationiteration,onwebkitanimationend,onsearch,onreset,o nwaiting,onvolumechange,onunload,ontimeupdate,onsuspend,onsubmit,onstorage,onstalled,onselect,onseek ing,onseeked,onscroll,onresize,onratechange,onprogress,onpopstate,onplaying,onplay,onpause,onpagesho w,onpagehide,ononline,onoffline,onwheel,onmousewheel,onmouseup,onmouseover,onmouseout,onmousemove,on mouseleave,onmouseenter,onmousedown,onmessage,onloadstart,onloadedmetadata,onloadeddata,onload,onkey up,onkeypress,onkeydown,oninvalid,oninput,onhashchange,onfocus,onerror,onended,onemptied,ondurationc hange,ondrop,ondragstart,ondragover,ondragleave,ondragenter,ondragend,ondrag,ondblclick,oncontextmen u,onclick,onchange,oncanplaythrough,oncanplay,onblur,onbeforeunload,onabort,getSelection,print,stop, open,showModalDialog,alert,confirm,prompt,find,scrollBy,scrollTo,scroll,moveBy,moveTo,resizeBy,resiz eTo,matchMedia,requestAnimationFrame,cancelAnimationFrame,webkitRequestAnimationFrame,webkitCancelAn imationFrame,webkitCancelRequestAnimationFrame,captureEvents,releaseEvents,atob,btoa,setTimeout,clea rTimeout,setInterval,clearInterval,TEMPORARY,PERSISTENT,getComputedStyle,getMatchedCSSRules,webkitCo nvertPointFromPageToNode,webkitConvertPointFromNodeToPage,webkitRequestFileSystem,webkitResolveLocal FileSystemURL,openDatabase,constructor
    Last edited by rnd me; 10-23-2013 at 11:36 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by rnd me View Post
    what object does the variable "b" belong to in the below code? or the variable "c", which you claim is a method?

    Code:
    function a(){
     var b=5;
     function c(){}
     return b;
    }
    b is a property of the a object and c is a method of the a object but since the a object gets discarded as soon as the code it contains finishes running there is no circumstance where you would ever need to reference them that way.

    You could write the code as follows which is identical to your version but makes the relationship more obvious.

    Code:
    function a() {
    this.b = 5;
    this.c = function() {}
    return this.b;
    }

    Also when you run JavaScript outside the browser the window object may not exist. It does in Adobe Air but all of the server side implementations of JavaScript I have looked at treat all globally defined properties and methods as belonging to the global object instead.

    See http://www.felgall.com/jstip182.htm for an article I wrote about how to determinew which object globally defined variables and functions in JavaScript belong to when the script runs in different environments and ways that some people have used to attempt to detect which is being used.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Quote Originally Posted by felgall View Post
    b is a property of the a object and c is a method of the a object but since the a object gets discarded as soon as the code it contains finishes running there is no circumstance where you would ever need to reference them that way.

    You could write the code as follows which is identical to your version but makes the relationship more obvious.
    that's just wrong, no way around it. i admit it was a trick question, but that doesn't mean there are not wrong answers.

    if what you are saying is correct, then this should show true:

    Code:
    function a() {
      var bb=5;
      this.b= 5;
        alert(this.b==this.bb);
       return this.b;
    }
    a();
    inside a function, vars are NOT a member of any object, unless they are explicitly published to an object.

    if as you claim "b is a property of the a object", then the following should also show true:

    Code:
    function a(){
     var b=5;
     alert( a.hasOwnProperty(b) )
     function c(){}
     return b;
    }
    
    a();

    while implicit globals get caught by the Global object, vars themselves cannot be iterated because they don't belong to any object. well, to be fair, there is a [SCOPE] object in the host environment, but JS doesn't provide access to that, so it doesn't change the facts of operation so far as we can concern ourselves as JS programmers.


    this privacy is the basis of "private" "properties" in constructors via closure, a pattern doug crockford calls "protected".
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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