attempting to understand the apply function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawnmoth

    attempting to understand the apply function

    Based on my understanding of the apply function, the following code
    snippet should, at the very least, not yield any errors.
    Unfortunately, it does, suggesting my understanding is... limited.
    Any ideas as to what's wrong?:

    <script>
    function simpleClass(val ue) {
    this.value = value;
    }

    (function() {
    this.displayVal ue = function() {
    alert("value = " + this.value);
    };

    this.changeValu e = function(value) {
    this.value = value;
    };
    }).apply(simple Class);

    a = new simpleClass("ab cd");
    a.changeValue(" efgh");
    a.displayValue( );
    </script>
  • David Mark

    #2
    Re: attempting to understand the apply function

    On Jan 14, 12:04 am, yawnmoth <terra1...@yaho o.comwrote:
    Based on my understanding of the apply function, the following code
    snippet should, at the very least, not yield any errors.
    Unfortunately, it does, suggesting my understanding is...  limited.
    Any ideas as to what's wrong?:
    >
    <script>
    function simpleClass(val ue) {
      this.value = value;
    >
    }
    I assume this is meant to be used as a constructor. Use
    "SimpleClas s" (but that is not the problem.)
    >
    (function() {
      this.displayVal ue = function() {
        alert("value = " + this.value);
      };
    >
      this.changeValu e = function(value) {
        this.value = value;
      };
    >
    }).apply(simple Class);
    This is very odd. Also, call could be used as you didn't provide any
    parameters to the function. Anyway, all you did was assign two
    properties to a Function object. I assume you meant to send the
    function's prototype instead.
    >
    a = new simpleClass("ab cd");
    Here you created an Object object, which will have a "value" property.
    a.changeValue(" efgh");
    The Object object has no such property.
    a.displayValue( );
    Same.
    </script>

    Comment

    • RobG

      #3
      Re: attempting to understand the apply function

      On Jan 14, 3:04 pm, yawnmoth <terra1...@yaho o.comwrote:
      Based on my understanding of the apply function, the following code
      snippet should, at the very least, not yield any errors.
      Unfortunately, it does, suggesting my understanding is... limited.
      Any ideas as to what's wrong?:
      >
      <script>
      function simpleClass(val ue) {
      this.value = value;
      >
      }
      >
      (function() {
      this.displayVal ue = function() {
      alert("value = " + this.value);
      };
      >
      this.changeValu e = function(value) {
      this.value = value;
      };
      >
      }).apply(simple Class);

      What you are doing here is calling the anonymous function and setting
      its this keyword as a reference to the simpleClass function object.
      Therefore, the changeValue property is added to the simpleClass
      object.

      Try:

      alert(typeof simpleClass.cha ngeValue); // --function


      a = new simpleClass("ab cd");
      a.changeValue(" efgh");
      "a" is a function object constructed from the simpleClass function.
      It only has the properties that were added in the constructor and
      those that are on the prototype chain. The simpleClass object isn't
      on a's prototype chain, so neither is the changeValue property that
      you added to simpleClass.

      alert(typeof a.changeValue); // --undefined


      Change the assignment to:


      (function() {
      ...
      }).apply(simple Class.prototype );


      and you'll have more luck. Presumably this is just play, I would not
      recommend such obfuscated code for assigning to the prototype
      property. Just write it:

      simpleClass.pro totype.changeVa lue = function(){ ... }


      --
      Rob

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: attempting to understand the apply function

        RobG wrote:
        On Jan 14, 3:04 pm, yawnmoth <terra1...@yaho o.comwrote:
        >a = new simpleClass("ab cd");
        >a.changeValue( "efgh");
        >
        "a" is a function object constructed from the simpleClass function.
        No, it is a reference to an Object object ...
        It only has the properties that were added in the constructor and
        .... that is augmented with these properties ...
        those that are on the prototype chain.
        .... and inherits those.
        The simpleClass object isn't on a's prototype chain, [...]
        Correct.


        PointedEars
        --
        Prototype.js was written by people who don't know javascript for people
        who don't know javascript. People who don't know javascript are not
        the best source of advice on designing systems that use javascript.
        -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

        Comment

        Working...