JavaScriptLang

Aus Wiki - Jochen Hammann
Zur Navigation springen Zur Suche springen


Aufruf einer überschriebenen Methode

Der folgende Code-Ausschnitt zeigt den Aufruf einer überschriebenen Methode einer Basisklasse aus der überschreibenden Methode der abgeleiteten Klasse heraus.

// -------------------- [Superclass] --------------------
function Shape() {
    this.x = 0;
    this.y = 0;
}

Shape.prototype.move = function (x, y) {
    this.x += x;
    this.y += y;
    console.info('Shape moved (' + this.x + ', ' + this.y + ')');
};

// -------------------- [Subclass] --------------------
function Rectangle() {
    Shape.call(this); // call super constructor.
}

// Subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

// Override method move()
Rectangle.prototype.move = function (x, y) {
    this.x += x;
    this.y += y;
    console.info('Rectanlge moved: (' + this.x + ', ' + this.y + ')');
    // Call base (overwritten) method.
    Shape.prototype.move.call(this, 2, 2);
};

// ----------------------------------------------------------------------

var rect = new Rectangle();
console.log('Is rect an instance of Rectangle? ' + (rect instanceof Rectangle)); // true
console.log('Is rect an instance of Shape? ' + (rect instanceof Shape)); // true
rect.move(1, 1);


Java Äquivalent zur JavaScript Funktion encodeURIComponent()

Der folgende Code-Ausschnitt zeigt eine Java Klasse, welche als Äquivalent zur JavaScript Funktion encodeURIComponent() dient.

/**
 * Utility class for JavaScript compatible UTF-8 encoding and decoding.
 * 
 * @see http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-output
 * @author John Topley 
 */
public class EncodingUtil
{
  /**
   * Decodes the passed UTF-8 String using an algorithm that's compatible with
   * JavaScript's <code>decodeURIComponent</code> function. Returns
   * <code>null</code> if the String is <code>null</code>.
   *
   * @param s The UTF-8 encoded String to be decoded
   * @return the decoded String
   */
  public static String decodeURIComponent(String s)
  {
    if (s == null)
    {
      return null;
    }

    String result = null;

    try
    {
      result = URLDecoder.decode(s, "UTF-8");
    }

    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
      result = s;  
    }

    return result;
  }

  /**
   * Encodes the passed String as UTF-8 using an algorithm that's compatible
   * with JavaScript's <code>encodeURIComponent</code> function. Returns
   * <code>null</code> if the String is <code>null</code>.
   * 
   * @param s The String to be encoded
   * @return the encoded String
   */
  public static String encodeURIComponent(String s)
  {
    String result = null;

    try
    {
      result = URLEncoder.encode(s, "UTF-8")
                         .replaceAll("\\+", "%20")
                         .replaceAll("\\%21", "!")
                         .replaceAll("\\%27", "'")
                         .replaceAll("\\%28", "(")
                         .replaceAll("\\%29", ")")
                         .replaceAll("\\%7E", "~");
    }

    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
      result = s;
    }

    return result;
  }  

  /**
   * Private constructor to prevent this class from being instantiated.
   */
  private EncodingUtil()
  {
    super();
  }
}