kwkbtr’s log

Skip to main content | 本文へ

(Last modified on Thu, 28 Mar 2013 02:09 JST)

Thu, 28 Mar 2013

JavaScriptでのクラスの実装の隠蔽 Clip to Evernote

こんなふうにしたらJavaScriptでクラスの実装を隠蔽できるんじゃないかなあと思った。

function() {

  // method list
  var methods = ["methodA", "methodB"];

  // implementation class
  var Impl = function() { ... };
  Impl.prototype = {
    methodA: function() { ... },
    methodB: function() { ... }
  };

  // the (interface) class
  var Foo = function() {
    var impl = new Impl(arguments);
    // delegate to the implementation class
    this.call_method = function(name, args) {
      return impl[name].apply(impl, args);
    };
  };

  // define method interface
  methods.forEach(function(name) {
    Foo.prototype[name] = function() {
      return this.call_method(name, arguments);
    };
  });

  return Foo;

};
Tags , | Comments on this entry

blog comments powered by Disqus

© 2009 KAWAKUBO Toru <web (atmark) kwkbtr.info>

Archives

2013‑Mar 2012‑Jun 2011‑Jul 2010‑Nov 2010‑Oct 2010‑Sep 2010‑Mar 2010‑Jan 2009‑Dec 2009‑Oct 2009‑Sep 2009‑Aug

Latest entries RSS 2.0 Atom 1.0 My tweets | kwkbtr.info Entries in English

See also: Twitter Facebook Flickr Delicious FriendFeed (feeding “kwkbtr’s log,” Twitter, Flickr, Delicious and Last.fm) Nike+ Mendeley Last.fm Tumblr

kwkbtrの早起き生活グラフ Powered by 早起き生活

Made with PyBlosxom