Ruby's Louvre

每天学习一点点算法

导航

Object.create

      function object(o) {
        o = Object(o);
        function F() {}
        F.prototype = o;
        var obj = new F();
        var methods = ["toString", "toLocaleString", "valueOf"];
        for (var i = 0; i < methods.length; i++)
          with ({ method: methods[i] })
        obj[method] = function () { return o[method].apply(o, arguments); };
        return obj;
      }

      var myStr = object("子");
      myStr.x = function (n) {
        var s = "";
        while (n--) s += this;
        return s;
      };
      alert(myStr.x(12)); 

posted on 2011-04-03 13:58  司徒正美  阅读(1390)  评论(0编辑  收藏  举报