Skip to content

Instantly share code, notes, and snippets.

@yu-tang
Created January 27, 2012 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yu-tang/1690096 to your computer and use it in GitHub Desktop.
Save yu-tang/1690096 to your computer and use it in GitHub Desktop.
CScript/WScript 環境の JScript 用クラス名取得関数 classof()
/*
CScript/WScript 環境の JScript 用クラス名取得関数 classof()
JScript では constructor.name が使えないので、下記をベースに改良。
Magnetiq - Finding Out Class Names of JavaScript Objects
http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects
何となくそれらしい文字列が取れていれば良いことにしておく。
*/
this.classof = function(obj) {
if (obj === null) return 'Null';
if (obj === undefined) return 'Undefined';
if (obj && obj.constructor && obj.constructor.toString) {
var arr = obj.constructor.toString().match(/function\s*(\w+)/);
if (arr && arr.length == 2) return arr[1];
}
if (obj && typeof(obj) == 'object' && obj.constructor === undefined)
if (obj === this) return 'Global'; else return 'ActiveX';
// どれにも該当しない場合は (文字列ではない) undefined を返します。
return undefined;
};
/********** 以下、テスト **********/
function test(type, expr) {
var obj = eval(expr);
WScript.Echo(type + ": " + expr);
try{WScript.Echo("- toString() returns: " + obj.toString());}catch(e){/*無視*/}
WScript.Echo("- typeof() returns: " + typeof(obj));
WScript.Echo("- classof() returns: " + classof(obj) + "\n");
}
function MyClass() {
}
test("Null", "null");
test("Integer", "42");
test("Boolean", "true");
test("String", '"Hello World!"');
test("Function", "MyClass");
test("Regular expression", "/Match this!/");
test("ActiveX object", 'new ActiveXObject("Scripting.Dictionary")');
test("Array object", "new Array(1, 2, 3)");
test("Date object", "new Date()");
test("Object object", "new Object()");
test("MyClass object", "new MyClass()");
test("Global object", "this");
test("Enumerator object", 'new Enumerator(new ActiveXObject("Scripting.Dictionary"))');
test("Undefined", "undefined");
Null: null
- typeof() returns: object
- classof() returns: Null
Integer: 42
- toString() returns: 42
- typeof() returns: number
- classof() returns: Number
Boolean: true
- toString() returns: true
- typeof() returns: boolean
- classof() returns: Boolean
String: "Hello World!"
- toString() returns: Hello World!
- typeof() returns: string
- classof() returns: String
Function: MyClass
- toString() returns: function MyClass() {
}
- typeof() returns: function
- classof() returns: Function
Regular expression: /Match this!/
- toString() returns: /Match this!/
- typeof() returns: object
- classof() returns: RegExp
ActiveX object: new ActiveXObject("Scripting.Dictionary")
- typeof() returns: object
- classof() returns: ActiveX
Array object: new Array(1, 2, 3)
- toString() returns: 1,2,3
- typeof() returns: object
- classof() returns: Array
Date object: new Date()
- toString() returns: Sat Jan 28 13:59:03 UTC+0900 2012
- typeof() returns: object
- classof() returns: Date
Object object: new Object()
- toString() returns: [object Object]
- typeof() returns: object
- classof() returns: Object
MyClass object: new MyClass()
- toString() returns: [object Object]
- typeof() returns: object
- classof() returns: MyClass
Global object: this
- typeof() returns: object
- classof() returns: Global
Enumerator object: new Enumerator(new ActiveXObject("Scripting.Dictionary"))
- toString() returns: [object Object]
- typeof() returns: object
- classof() returns: Enumerator
Undefined: undefined
- typeof() returns: undefined
- classof() returns: Undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment