Created
November 17, 2010 11:58
-
-
Save Songmu/703314 to your computer and use it in GitHub Desktop.
Dateをがんばって継承してみようとしたメモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dateをがんばって継承してみようとしたメモ | |
* based on: | |
* Prototypal Inheritance by Douglas Crockford | |
* http://javascript.crockford.com/prototypal.html | |
*/ | |
//Dateを拡張していろいろはまったので | |
//http://nanto.asablo.jp/blog/2005/10/24/118564 | |
//http://nanto.asablo.jp/blog/2006/10/18/566348 | |
//http://groups.google.co.jp/group/comp.lang.javascript/browse_thread/thread/7ef79470e8fba7a5/e8fb95e4a7693e47 | |
//http://d.hatena.ne.jp/send/20070622/p1 | |
function object(o) {//インスタンスを複製するためのものでしかない | |
if (o == undefined) return {}; | |
switch (o.constructor) { | |
case String: | |
case Boolean: | |
case Array: | |
return objectWithMethods(Object(o), []); | |
case Number: | |
return objectWithMethods(Object(o), | |
"toFixed toExponential toPrecision".split(" ")); | |
case Date: | |
return objectWithMethods(o, | |
("toDateString toTimeString toLocaleDateString toLocaleTimeString " + | |
"getTime getFullYear getUTCFullYear getMonth getUTCMonth getDate " + | |
"getUTCDate getDay getUTCDay getHours getUTCHours getMinutes " + | |
"getUTCMinutes getSeconds getUTCSeconds getMilliseconds " + | |
"getUTCMilliseconds getTimezoneOffset setTime setMilliseconds " + | |
"setUTCMilliseconds setSeconds setUTCSeconds setMinutes " + | |
"setUTCMinutes setHours setUTCHours setDate setUTCDate setMonth " + | |
"setUTCMonth setFullYear setUTCFullYear toUTCString").split(" ")); | |
case RegExp: | |
return objectWithMethods(o, "exec test".split(" ")); | |
case Function: | |
return objectWithMethods(o, "apply call".split(" ")); | |
} | |
var F = function () {}; | |
F.prototype = o; | |
return new F(); | |
} | |
function objectWithMethods(o, methods) { | |
function F() {} | |
F.prototype = o; | |
var obj = new F(); | |
methods.push("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; | |
} | |
//++// | |
function p(str){ | |
document.write(str + "<br>"); | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
//////////////////////////////////////////////////////////////////////////////// | |
function JaDate(){ | |
var dd; | |
if(arguments.length == 0){ | |
dd = object(new Date()); | |
} | |
else{ | |
var tmp_str = Array.prototype.join.apply(arguments,["','"]); | |
tmp_str = "'" + tmp_str + "'"; | |
eval("dd = object(new Date(" + tmp_str+ "))"); | |
} | |
dd.__proto__ = JaDate.prototype; | |
dd.name = "hohogegege"; | |
return dd; | |
} | |
JaDate.prototype = object(new Date());//一応 | |
JaDate.prototype.constructor = JaDate; | |
JaDate.prototype.getName = function(){ | |
return this.name; | |
}; | |
JaDate._prototype = function(){ | |
this.value; | |
} | |
var ja = JaDate();//newは不要 というかnewされるとprototypeから呼び出されるので引数を無視してしまう。 | |
//引数をどうにかしてapplyする //Dateの場合むずかしいか | |
//http://nanto.asablo.jp/blog/2006/10/18/566348 | |
//"not generic" (汎用的ではない) オブジェクトのプロトタイプが String 型のオブジェクトであったとしても呼び出せない Stringのメソッド | |
//var hoge = new JaDate(); | |
p(ja.getDate()); | |
p(ja.constructor); | |
p(Object.prototype.toString.apply(ja));//object object | |
p(ja.getName()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment