Don't understand these code.

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

    Don't understand these code.

    Hi All

    These code are part of Prototype. There are something that I don't
    understand, would you please explain it for me?

    .......
    toJSON: function(object ) {
    var type = typeof object;
    switch (type) {
    case 'undefined':
    case 'function':
    case 'unknown': return;
    case 'boolean': return object.toString ();
    }

    if (object === null) return 'null';
    if (object.toJSON) return object.toJSON() ;
    --------------------------------------if toJSON is a function name,
    it is a function that extends the Object
    if object.toJSON is true and the return object.toJSON() , what is the
    mean? It is like a loop.
    if (Object.isEleme nt(object)) return;

    var results = [];
    for (var property in object) {
    var value = Object.toJSON(o bject[property]);
    ---------------------------------I think the value should be the value
    of the property.But from the code above, I can not understand why
    value can be the value of the property
    if (!Object.isUnde fined(value))
    results.push(pr operty.toJSON() + ': ' + value);
    }

    return '{' + results.join(', ') + '}';
    },

    Thanks,
  • Tom de Neef

    #2
    Re: Don't understand these code.

    "None" wrote
    ......
    toJSON: function(object ) {
    var type = typeof object;
    switch (type) {
    case 'undefined':
    case 'function':
    case 'unknown': return;
    case 'boolean': return object.toString ();
    }
    >
    if (object === null) return 'null';
    if (object.toJSON) return object.toJSON() ;
    --------------------------------------if toJSON is a function name,
    it is a function that extends the Object
    if object.toJSON is true and the return object.toJSON() , what is the
    mean?
    object.toJSON will be the result of a function call. If the argument to that
    function is of type boolean, the result will be a string, otherwise the
    result will be undefined (return without a value).

    The if statement checks if the value of toJSON is true (i.e. if
    toBoolean(expre ssion) is true. If it is undefined, that will be boolean
    false and nothing happens. In the other case the toJSON value (a string)
    will be returned.

    Tom


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Don't understand these code.

      None <jdxyw2004@gmai l.comwrites:
      toJSON: function(object ) {
      "toJSON" is a function somewhere that that takes a value as argument
      and return a string value containing a JSON literal representing the
      original value, if possible.
      var type = typeof object;
      switch (type) {
      case 'undefined':
      case 'function':
      case 'unknown': return;
      case 'boolean': return object.toString ();
      }
      >
      if (object === null) return 'null';
      At this point we know that the value is not a function or boolean,
      and is not undefined or "unknown" (a non-standard type returned by
      some browsers' host objects).
      if (object.toJSON) return object.toJSON() ;
      --------------------------------------if toJSON is a function name,
      it is a function that extends the Object
      If the original value, converted to an object if necessary (e.g., a
      number or a string) has a property called "toJSON", then call the
      value of that property as a method and return the result.

      This will catch, e.g., if String.prototyp e or Array.prototype has had
      a "toJSON" method added, or if a custom object has its own method.
      if object.toJSON is true and the return object.toJSON() , what is the
      mean?
      If the property object.toJSON's value, converted to a boolean, is true,
      then return the result of calling object.toJSON() .
      Converting a function to a boolean gives "true". Converting the value
      of an undefined property gives "false".
      It is like a loop.
      It's a recursive call. Remember, the "toJSON" method on the object is
      not the same as the current toJSON function (or at least it shouldn't
      be, since the current one expects an argument).
      if (Object.isEleme nt(object)) return;
      >
      var results = [];
      for (var property in object) {
      var value = Object.toJSON(o bject[property]);
      ---------------------------------I think the value should be the value
      of the property.But from the code above, I can not understand why
      value can be the value of the property
      Not understood.
      I'm assuming that Object.toJSON is the function we are currently looking
      at.
      This loop runs through the enumerable properties of the original object
      and converts them to a JSON literal string, one by one, using this method.
      Then it collects them in an array, and creates a JSON object literal
      containing those properties.
      if (!Object.isUnde fined(value))
      results.push(pr operty.toJSON() + ': ' + value);
      }
      >
      return '{' + results.join(', ') + '}';
      },
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...