Functions as objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • relaxedrob@optushome.com.au

    Functions as objects

    Howdy All!

    I am still getting my head around a few base concepts. Any comments or
    criticisms on the below definitions would be most welcome!

    A function is an object.

    JavaScript objects have properties:
    - a var (which is an object or a primitive);
    - a method (which is a function assigned to the object).

    Note that since methods are functions and functions are objects we
    could re-write the above to reflect a different view of an object's
    properties.

    JavaScript objects have properties:
    - other objects (declared as a var or method);
    - primitive vars.

    Thanks for any advice!

    Rob
    :)
  • Lasse Reichstein Nielsen

    #2
    Re: Functions as objects

    relaxedrob@optu snet.com.au (relaxedrob@opt ushome.com.au) writes:
    [color=blue]
    > I am still getting my head around a few base concepts. Any comments or
    > criticisms on the below definitions would be most welcome!
    >
    > A function is an object.[/color]

    Correct. It is an object, that internally implements the "[[call]]"
    method.
    [color=blue]
    > JavaScript objects have properties:
    > - a var (which is an object or a primitive);[/color]

    Don't call it a "var". That keyword is used for declaring local
    variables, not properties of objects.
    [color=blue]
    > - a method (which is a function assigned to the object).[/color]

    It is a property with a value that is a function. We can call it a
    "method" if we want.
    [color=blue]
    > Note that since methods are functions and functions are objects we
    > could re-write the above to reflect a different view of an object's
    > properties.
    >
    > JavaScript objects have properties:
    > - other objects (declared as a var or method);[/color]

    Just "other objects (functions and non-functions)".
    [color=blue]
    > - primitive vars.[/color]

    A different approach is:

    A Javascript *value* is either a primitive value (number, string,
    boolean, undefined or null), or it is an *object*.

    *Objects* have properties, accessible by name, which contain
    *values*.

    Some *objects* are *functions*, and can be called.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: Functions as objects

      > A function is an object.[color=blue]
      >
      > JavaScript objects have properties:
      > - a var (which is an object or a primitive);
      > - a method (which is a function assigned to the object).[/color]

      Not exactly. Vars are variables that are bound to a function. These are distinct
      (or at least should be) from the members of an object. A member whose value is a
      function can be considered a method, although the language itself does not make
      this distinction.

      Common members of functions include the .prototype object and the .apply method.



      Comment

      • Robert Bram

        #4
        Re: Functions as objects

        Hi All!

        I want to thank both David and Lasse for your input to this thread. Here
        is the result of my thinking now - once again I welcome further comments
        or criticisms.

        ============
        Here are two ways to think about JavaScript objects.

        1) The first is in terms of typical object oriented concepts. An object
        encapsulates.
        - data (called properties in JavaScript); and
        - behaviour (called methods in JavaScript).

        This way of thinking is useful because it enables a discussion of
        objects in a non-language specific manner.

        2) The second way to think about JavaScript objects involves us
        understanding that in JavaScript, a function is also an object.

        JavaScript objects have properties that we can refer to by name.

        Each property has a value.

        A Javascript value is either:
        - a primitive (number, string, boolean, undefined or null); or
        - an object (function or non-function).

        This way of thinking is useful because it helps us to understand that a
        JavaScript object definition (i.e. a class in object oriented
        terminology) is any function that assigns itself properties.


        Further concepts.

        A prototype is an object property that belongs to a constructor function
        and is automatically created when the function begins assigning
        properties to itself. The prototype is JavaScript's way of remembering
        what properties an object definition has. Object definitions (i.e.
        constructor functions) may inherit or copy the prototype of other object
        definitions - meaning that a constructor may define itself as having the
        same set of properties as another constructor.

        A var is a local variable. A local variable is a value that may only be
        referenced by code within a function or script, depending on whether the
        var was declared within a function or script respectively.
        ============

        Rob
        :)

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Robert Bram

          #5
          Re: Functions as objects

          Hi All!

          I want to thank both David and Lasse for your input to this thread. Here
          is the result of my thinking now - once again I welcome further comments
          or criticisms.

          ============
          Here are two ways to think about JavaScript objects.

          1) The first is in terms of typical object oriented concepts. An object
          encapsulates.
          - data (called properties in JavaScript); and
          - behaviour (called methods in JavaScript).

          This way of thinking is useful because it enables a discussion of
          objects in a non-language specific manner.

          2) The second way to think about JavaScript objects involves us
          understanding that in JavaScript, a function is also an object.

          JavaScript objects have properties that we can refer to by name.

          Each property has a value.

          A Javascript value is either:
          - a primitive (number, string, boolean, undefined or null); or
          - an object (function or non-function).

          This way of thinking is useful because it helps us to understand that a
          JavaScript object definition (i.e. a class in object oriented
          terminology) is any function that assigns itself properties.


          Further concepts.

          A prototype is an object property that belongs to a constructor function
          and is automatically created when the function begins assigning
          properties to itself. The prototype is JavaScript's way of remembering
          what properties an object definition has. Object definitions (i.e.
          constructor functions) may inherit or copy the prototype of other object
          definitions - meaning that a constructor may define itself as having the
          same set of properties as another constructor.

          A var is a local variable. A local variable is a value that may only be
          referenced by code within a function or script, depending on whether the
          var was declared within a function or script respectively.
          ============

          Rob
          :)

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • relaxedrob@optushome.com.au

            #6
            Re: Functions as objects

            Hi All!

            I want to thank both David and Lasse for your input to this thread. Here
            is the result of my thinking now - once again I welcome further comments
            or criticisms.

            ============
            Here are two ways to think about JavaScript objects.

            1) The first is in terms of typical object oriented concepts. An object
            encapsulates.
            - data (called properties in JavaScript); and
            - behaviour (called methods in JavaScript).

            This way of thinking is useful because it enables a discussion of
            objects in a non-language specific manner.

            2) The second way to think about JavaScript objects involves us
            understanding that in JavaScript, a function is also an object.

            JavaScript objects have properties that we can refer to by name.

            Each property has a value.

            A Javascript value is either:
            - a primitive (number, string, boolean, undefined or null); or
            - an object (function or non-function).

            This way of thinking is useful because it helps us to understand that a
            JavaScript object definition (i.e. a class in object oriented
            terminology) is any function that assigns itself properties.


            Further concepts.

            A prototype is an object property that belongs to a constructor function
            and is automatically created when the function begins assigning
            properties to itself. The prototype is JavaScript's way of remembering
            what properties an object definition has. Object definitions (i.e.
            constructor functions) may inherit or copy the prototype of other object
            definitions - meaning that a constructor may define itself as having the
            same set of properties as another constructor.

            A var is a local variable. A local variable is a value that may only be
            referenced by code within a function or script, depending on whether the
            var was declared within a function or script respectively.
            ============

            Rob
            :)

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Functions as objects

              Robert Bram <relaxedrob@opt usnet.com.au> writes:

              [color=blue]
              > This way of thinking is useful because it helps us to understand that a
              > JavaScript object definition (i.e. a class in object oriented
              > terminology) is any function that assigns itself properties.[/color]

              In Javascript, the word "class" is, like "method", only something that
              exists in the eyes of the user. Internally, a method is just a property
              that happens to be a function. And "class" is usually used about a function
              that happens to be used as a constructor.

              Examples of built-in constructor functions: Object, Array, Number,
              String, Date, RegExp. These are *functions*, but if you use them with
              the "new" operator, they generate objects that have similar
              structures. The notation ("new Foo()") and use (creating new similar
              objects) make us think about them as we would about classes in
              class-based object oriented languages. But in Javascript, any function
              can be a constructor/class.

              Example:
              function Point(x,y) {
              this.x=x;
              this.y=y;
              }
              This function is meant to be a constructor. When you use it as
              new Point(10,20)
              the "new" operator creates a new object, and calls Point so that the
              "this" keyword refers to the new object. It calls Point just as any
              other function.

              You could get the same result with "Point.call (new Object(),10,20) ",
              (in this case only, there are more details to the "new" operator).

              You can write
              new func()
              for any function func.
              [color=blue]
              > A prototype is an object property that belongs to a constructor function[/color]

              I.e., any function. Any Javascript function initially has a property
              called "prototype" , that refers to an object.
              [color=blue]
              > and is automatically created when the function begins assigning
              > properties to itself.[/color]

              It is created when the function is.

              var foo = function(){};
              alert(typeof foo.prototype);
              [color=blue]
              > The prototype is JavaScript's way of remembering
              > what properties an object definition has. Object definitions (i.e.
              > constructor functions) may inherit or copy the prototype of other object
              > definitions - meaning that a constructor may define itself as having the
              > same set of properties as another constructor.[/color]

              This got me confuzed. Constructor functions (i.e., any function) uses
              their prototype object for properties that will be shared by the
              created objects. They are not properties of the constructor itself.

              Any user-created Javascript object has an prototype reference. When
              you look for a property, say "obj.prop", then the property is looked
              for in the object itself. If it is not found, it is looked for in the
              object's prototype, and so forth.

              When you create a new object with the "new" operator and a function,
              the new object's prototype reference will point to the value of the
              function's prototype property.

              Try looking at what happens here:
              ---
              function Foo(){};
              Foo.prototype.x = 42;
              var x = new Foo();
              alert(x.x); // 42

              x.x = 37;
              alert(x.x); // 37

              delete x.x; // remove the property from the object
              alert(x.x); // 42 again, the prototype wasn't affected.

              Foo.prototype.x = 37;
              alert(x.x); // 37. The prototype object can be changed dynamically too.

              Foo.prototype = {x:4,y:87}; // overwrite prototype with new object
              var y = new Foo();

              alert(x.x); // 37, the prototype reference of old objects are unchanged
              alert(y.x); // 4
              alert(y.y); // 87
              ---


              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Robert Mark Bram

                #8
                Re: Functions as objects

                Hi Lasse!
                [color=blue][color=green]
                > > A prototype is an object property that belongs to a constructor function[/color]
                >
                > I.e., any function. Any Javascript function initially has a property
                > called "prototype" , that refers to an object.
                >[color=green]
                > > and is automatically created when the function begins assigning
                > > properties to itself.[/color]
                >
                > It is created when the function is.[/color]

                Good. I understand this now.
                [color=blue][color=green]
                > > The prototype is JavaScript's way of remembering
                > > what properties an object definition has. Object definitions (i.e.
                > > constructor functions) may inherit or copy the prototype of other object
                > > definitions - meaning that a constructor may define itself as having the
                > > same set of properties as another constructor.[/color]
                >
                > This got me confuzed. Constructor functions (i.e., any function) uses
                > their prototype object for properties that will be shared by the
                > created objects. They are not properties of the constructor itself.[/color]

                This is reminiscent of the Matrix! Let me see if I understand right ...

                function Bar(){
                this.prototype;
                }
                Bar.prototype;

                OK then.. Bar is an object - actually it is an instance of Function.
                Function defines a property called "prototype" . This is why Bar.prototype
                works.

                this.prototype doesn't work because "this" is a reference to "a" Bar object.
                The Bar object is not a Function and therefore does not have a prototype.

                How does that sound?

                Rob
                :)


                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: Functions as objects

                  "Robert Mark Bram" <robert.bram@yo urshoesinfotech .monash.edu.au> writes:
                  [color=blue]
                  > This is reminiscent of the Matrix! Let me see if I understand right ...[/color]

                  "There is no function^H^H^H^ H^H^H^H^Hspoon" .
                  [color=blue]
                  > function Bar(){
                  > this.prototype;
                  > }
                  > Bar.prototype;
                  >
                  > OK then.. Bar is an object - actually it is an instance of Function.[/color]

                  Yes. Its prototype reference (not to be confuzed with its property called
                  "prototype" ) refers to Function's "prototype" property's value.

                  Proof:
                  var result = "";
                  var x = function() {}; // create a function
                  result += x.foo; // see that its "foo" property is undefined
                  Function.protot ype.foo = 42; // add a foo property to Function.protot ype
                  result += "/"+x.foo; // see that x's "foo" property is now defined
                  alert(result);
                  [color=blue]
                  > Function defines a property called "prototype" . This is why Bar.prototype
                  > works.[/color]

                  The prototype property of a function is *not* there because there is a
                  Function.protot ype.prototype
                  There isn't. A newly created function is assigned a brand new object
                  as a prototype property.

                  If the prototype property came through the prototype reference, then
                  all functions would share the same objects as their prototype property.
                  They don't, each function has its own object.
                  [color=blue]
                  > this.prototype doesn't work because "this" is a reference to "a" Bar object.[/color]

                  (well, it "works" and gives "undefined" :)

                  The "this" in Bar will most likely refer to the global object, which don't
                  have a prototype property.
                  [color=blue]
                  > The Bar object is not a Function and therefore does not have a prototype.[/color]

                  Add "probably" before "does not". :)
                  But otherwise, yes.
                  [color=blue]
                  > How does that sound?[/color]

                  Almost correct. :)
                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Douglas Crockford

                    #10
                    Re: Functions as objects

                    > This is reminiscent of the Matrix! Let me see if I understand right ...[color=blue]
                    >
                    > function Bar(){
                    > this.prototype;
                    > }
                    > Bar.prototype;
                    >
                    > OK then.. Bar is an object - actually it is an instance of Function.
                    > Function defines a property called "prototype" . This is why Bar.prototype
                    > works.
                    >
                    > this.prototype doesn't work because "this" is a reference to "a" Bar object.
                    > The Bar object is not a Function and therefore does not have a prototype.[/color]

                    There are actually two prototype properties. Some object will have both.

                    First, all functions have a .prototype object, which is usually a container of
                    instance methods. All functions have one because any function could potentially
                    be a constructor. JavaScript does not distinguish between functions and
                    constructors, which is a minor problem. That means that you must use 'new' when
                    calling a constructor.

                    Second, all objects have an invisible [[proto]] member. If a search for a name
                    in an object fails, the name will then be sought again from the [[proto]]
                    object. If that fails, it will search the [[proto]] object's [[proto]] object,
                    and so on. This supports the reuse pattern in JavaScript.

                    The [[proto]] member is set at the creation on the object and cannot be changed
                    or directly examined. (Netscape 4 does allow access to __proto__, but this is
                    non-standard.) The 'new' unary operator makes a new empty object with [[proto]]
                    set to constructor.pro totype, then binds it to this while calling the
                    constructor function.

                    It is almost always wrong to refer to this.prototype .



                    Comment

                    Working...