Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
// class.inheritance.js function SuperClass() {} function SubClass() {} SubClass.prototype = new SuperClass; o = new SuperClass; p = new SubClass; WScript.echo( o instanceof SuperClass ); // true WScript.echo( p instanceof SuperClass ); // true WScript.echo( o instanceof SubClass ); // false WScript.echo( p instanceof SubClass ); // true
// class.initialize.call.js function Super( i ) { this.mSuper = ( "init: " + i ); } function Sub( i ) { Super.call( this, ( "arg " + i ) ); this.mSub = ( "init: " + i ); } Sub.prototype = new Super; o = new Sub( "new" ); WScript.echo( o.mSuper ); // init: arg new WScript.echo( o.mSub ); // init: new
// class.constructor.js function Super() {} function Suba() {} Suba.prototype = new Super; function Subb() {} Subb.prototype = new Super; Subb.prototype.constructor = Subb; o = new Suba; p = new Subb; q = new o.constructor; r = new p.constructor; WScript.echo( o.constructor ); // function Super() {} WScript.echo( p.constructor ); // function Subb() {} WScript.echo( q.constructor ); // function Super() {} WScript.echo( r.constructor ); // function Subb() {} WScript.echo( o instanceof Super ); // true WScript.echo( o instanceof Suba ); // true WScript.echo( p instanceof Super ); // true WScript.echo( p instanceof Subb ); // true WScript.echo( q instanceof Super ); // true WScript.echo( q instanceof Suba ); // false WScript.echo( r instanceof Super ); // true WScript.echo( r instanceof Subb ); // true
// class.js function SuperClass() { this.attr = "hi"; this.hi = function() { return this.attr; }; } function SubClass() { this.hello = function() { return "hello"; }; } SubClass.prototype = new SuperClass; p = new SubClass; WScript.echo( p.hi() ); // hi WScript.echo( p.hello() ); // hello
// class.unnamedClass.js klass = function() { this.hi = function() { return this.attr; }; }; superKlass = klass; subKlass = function() { this.hello = function() { return "hello"; }; }; subKlass.prototype = new superKlass; superKlass.prototype.attr = "hi"; o = new klass; p = new superKlass; q = new subKlass; WScript.echo( o.hi() ); // hi WScript.echo( p.hi() ); // hi WScript.echo( q.hi() ); // hi WScript.echo( q.hello() ); // hello p.attr = "hallo" WScript.echo( o.hi() ); // hi WScript.echo( p.hi() ); // hallo WScript.echo( q.hi() ); // hi
// class.scope.js function SuperClass() { this.attr = "hi"; this.hi = function() { return this.attr; }; } function SubClass() { this.hello = function() { return this.attr; }; } SubClass.prototype = new SuperClass; p = new SubClass; WScript.echo( p.hi() ); // hi WScript.echo( p.hello() ); // hi
// class.initialize.scope.super.js function Super() { var hi = "hi"; var hello = "hello"; this.foo = function() { return hi; }; this.bar = this.foo; this.baz = function() { return hello; }; } function Sub() { var hello = "HI"; this.bar = function() { return hi; }; } Sub.prototype = new Super; hi = "..."; o = new Sub; WScript.echo( o.foo() ); // hi WScript.echo( o.bar() ); // ... WScript.echo( o.baz() ); // hello
// class.initialize.scope.sub.js function Super() { this.foo = function() { return hi; }; this.bar = this.foo; this.baz = function() { return this.bar(); }; } function Sub() { var hi = "hi"; this.bar = function() { return hi; }; } Sub.prototype = new Super; hi = "..."; o = new Sub; WScript.echo( o.foo() ); // ... WScript.echo( o.bar() ); // hi WScript.echo( o.baz() ); // hi
// class.localclass.scope.js function hoge() { var v = "local"; var o = new function() { var w = "class local"; this.foo = function() { return v; }; this.bar = function() { return w; }; }; return Array( o.foo(), o.bar() ); } v = w = "..."; a = hoge(); WScript.echo( a.shift() ); // local WScript.echo( a.shift() ); // class local
// class.private.js function Klass() { this.foo = function() { return bar(); } function bar() { return v; } var v = "private"; } k = new Klass; WScript.echo( k.foo() ); // private //WScript.echo( k.bar() ); // error
// class.classmethod.js function Klass() { this.methodA = function() {}; // this.prototype.methodB = function() {}; this.constructor.prototype.methodC = function() {}; } Klass.prototype.methodD = function() {}; Klass.methodE = function() {}; WScript.echo( typeof( Klass.methodA ) ); // undefined //WScript.echo( typeof( Klass.methodB ) ); // WScript.echo( typeof( Klass.methodC ) ); // undefined WScript.echo( typeof( Klass.methodD ) ); // undefined WScript.echo( typeof( Klass.methodE ) ); // function o = new Klass; WScript.echo( typeof( o.methodA ) ); // function //WScript.echo( typeof( o.methodB ) ); // WScript.echo( typeof( o.methodC ) ); // function WScript.echo( typeof( o.methodD ) ); // function WScript.echo( typeof( o.methodE ) ); // undefined WScript.echo( typeof( Klass.methodA ) ); // undefined //WScript.echo( typeof( Klass.methodB ) ); // WScript.echo( typeof( Klass.methodC ) ); // undefined WScript.echo( typeof( Klass.methodD ) ); // undefined WScript.echo( typeof( Klass.methodE ) ); // function
// class.classmethod.inheritance.js function Klassa() {} Klassa.methodA = function() {}; function Klassb() {} Klassb.prototype = new Klassa; Klassb.methodB = function() {}; WScript.echo( typeof( Klassa.methodA ) ); // function WScript.echo( typeof( Klassa.methodB ) ); // undefined WScript.echo( typeof( Klassb.methodA ) ); // undefined WScript.echo( typeof( Klassb.methodB ) ); // function
// this.context.js function KlassA() {} var value = "global"; this.value = "this"; for( var i in this ) { WScript.echo( i + ": " + this[ i ] ); } "abc".replace( /./, function() { WScript.echo( "in replace: " + this.value ); // this } ); function KlassB() { this.value = "Klass B"; this.foo = function() { return this.value; }; function KlassC() { this.value = "Klass C"; this.foo = function() { return this.value; }; } this.bar = function() { var o = new KlassC; var m = o.foo; return m(); }; this.baz = function() { var o = new KlassC; this.m = o.foo; return this.m(); }; } o = new KlassB; WScript.echo( "o.foo(): " + o.foo() ); // Klass B b = o.foo; WScript.echo( "method object(?): " + b() ); // this WScript.echo( "o.bar(): " + o.bar() ); // this WScript.echo( "o.baz(): " + o.baz() ); // Klass B
// scope.var.js function foo() { for( i = 1; i <= 10; i++ ) { WScript.echo( i ); } } function bar() { var i; for( i = 1; i <= 10; i++ ) { WScript.echo( i ); } } function baz() { for( var i = 1; i <= 10; i++ ) { WScript.echo( i ); } } i = 100; foo(); WScript.echo( "value of " + i ); // value of 11 i = 100; bar(); WScript.echo( "value of " + i ); // value of 100 i = 100; baz(); WScript.echo( "value of " + i ); // value of 100
// function.scope.outcall.js function sub() { v = "sub"; } function foo() { var v = "foo"; sub(); return v; } function bar( iFunc ) { var v = "bar"; iFunc(); return v; } function baz() { var v = "baz"; return Array( sub, v ); } function getfunc() { var v = "getfunc"; var f = function() { v = "func in getfunc"; }; return f; } function bab() { var v = "bab"; var f = getfunc(); f(); return v; } v = "top"; r = foo(); WScript.echo( v + " : " + r ); // sub : foo v = "top"; r = bar( sub ); WScript.echo( v + " : " + r ); // sub : bar v = "top"; a = baz(); f = a.shift(); r = a.shift(); f(); WScript.echo( v + " : " + r ); // sub : baz v = "top"; r = bab( sub ); WScript.echo( v + " : " + r ); // top : bab
// function.scope.incall.js function foo() { var v = "foo"; var f = function() { v = "func in foo"; }; f(); return v; } function sub( iFunc ) { var v = "sub"; iFunc(); } function bar() { var v = "bar"; var f = function() { v = "func in bar"; }; sub( f ); return v; } function baz() { var v = "baz"; var f = function() { v = "func in baz"; }; return Array( f, v ); } r = foo(); WScript.echo( r ); // func in foo r = bar( sub ); WScript.echo( r ); // func in bar a = baz(); f = a.shift(); r = a.shift(); f(); WScript.echo( r ); // baz
// eval.scope.js eval( "function foo(){};" ); (function() { eval( "function bar(){};" ); })(); WScript.echo( typeof( foo ) ); // function WScript.echo( typeof( bar ) ); // undefined
// closure.localclass.js function Klass() { var varA = "class"; var varC = "class"; this.method = function( c ) { var varB = "method"; var k = function() { var varC = "local class"; this.foo = function() { return varA; }; this.bar = function() { return varB; }; this.baz = function() { return varC; }; }; c( new k ); }; } ( function() { var s = new Klass; var p; s.method( function( o ) { WScript.echo( o.foo() ); // class WScript.echo( o.bar() ); // method WScript.echo( o.baz() ); // local class p = o; } ); WScript.echo( p.foo() ); // class WScript.echo( p.bar() ); // method WScript.echo( p.baz() ); // local class } )();
// closure.continue.js a = [ 1, 2, 3 ] b = function( iBlock ) { iBlock(); } for( var i in a ) { var item = a[ i ]; b( function() { if ( 2 === item ) { continue; } // error if ( 3 === item ) { break; } // error } ); WScript.echo( item ); }
// typeof.js WScript.echo( typeof( 1 ) ); // number WScript.echo( typeof( "a" ) ); // string WScript.echo( typeof( true ) ); // boolean WScript.echo( typeof( function(){} ) ); // function WScript.echo( typeof( null ) ); // object WScript.echo( typeof( undefined ) ); // undefined WScript.echo( typeof( new Number ) ); // object WScript.echo( typeof( new String ) ); // object WScript.echo( typeof( new Boolean ) ); // object WScript.echo( typeof( new Object ) ); // object WScript.echo( typeof( /abc/ ) ); // object WScript.echo( /abc/ instanceof RegExp ); // -1 WScript.echo( typeof( [1, 2] ) ); // object WScript.echo( [1, 2] instanceof Array ); // -1 WScript.echo( typeof( {one:1} ) ); // object WScript.echo( {one:1} instanceof Array ); // 0 WScript.echo( "\n*** Property: " ); WScript.echo( "prototype" in {a:1} ); // 0 WScript.echo( "prototype" in new Object ); // 0 WScript.echo( "prototype" in Object ); // -1 WScript.echo( "constructor" in {a:1} ); // -1 WScript.echo( "constructor" in new Object ); // -1 WScript.echo( "constructor" in Object ); // -1 WScript.echo( {one:1}.constructor ); // func... WScript.echo( (new Object).constructor ); // func...
// in.js m = "method"; //WScript.echo( m in 123 ); o = new Number; WScript.echo( m in o ); //WScript.echo( m in "abc" ); o = new String; WScript.echo( m in o ); //WScript.echo( m in true ); o = new Boolean; WScript.echo( m in o ); WScript.echo( m in function(){} ); WScript.echo( m in /re/ ); //WScript.echo( m in null ); //WScript.echo( m in undefined ); WScript.echo( m in new Array ); WScript.echo( m in Object ); o = { method : 1 }; WScript.echo( m in o ); a = [1]; WScript.echo( "0" in a );
// throw.try-catch-finally.js try { try { null.method(); } catch( e ) { WScript.echo( "catch: " + e.message ); throw {message:"throw"}; } finally { WScript.echo( "finally" ); } } catch( e ) { WScript.echo( "catch: " + e.message ); }
// throw.error.js function print_e( e ) { WScript.echo( "description:\t" + e.description ); WScript.echo( "message:\t" + e.message ); /* name には以下の値が含まれる、らしい。 ConversionError RangeError ReferenceError RegExpError SyntaxError TypeError URIError */ WScript.echo( "name:\t\t" + e.name ); WScript.echo( "number:\t\t" + e.number ); WScript.echo( "constructor:\t" + e.constructor ); WScript.echo( "collectiopn: " ); for( var i in e ) { WScript.echo( "> " + i + ": " + e[i] ); } WScript.echo(); } try { null.method(); } catch( e ) { print_e( e ); } try { throw new Error; } catch( e ) { print_e( e ); } try { throw new Error( 0, "except" ); } catch( e ) { print_e( e ); } try { throw "except"; } catch( e ) { print_e( e ); } try { throw { name:"name" }; } catch( e ) { print_e( e ); }
// interpreter.error.null.js try { null.aaa = 1; } catch( e ) { throw 1; } // interpreter.error.null.js(5, 2) Microsoft JScript 実行時エラー: catch ステー // トメントでは適用されますが、throw ステートメントでは適用されません。
// hash.object.js h = new Array; h[ 1 ] = "number"; h[ "1" ] = "string"; a = new String( "1" ); h[ a ] = "String"; b = new String( "1" ); h[ b ] = "STRING"; c = new String( "01" ); h[ c ] = "STRING 01"; d = /abc/; h[ d ] = "regexp abc"; e = /ABC/; h[ e ] = "regexp ABC"; function foo( arg ) { var v = arg; this.toString = function() { return "" + v; }; } f = new foo( 1 ); h[ f ] = "foo a"; g = new foo( 2 ); h[ g ] = "foo b"; WScript.echo( h[ 1 ] ); // STRING WScript.echo( h[ "1" ] ); // STRING WScript.echo( h[ a ] ); // STRING WScript.echo( h[ b ] ); // STRING WScript.echo( h[ c ] ); // STRING 01 WScript.echo( h[ d ] ); // regexp abc WScript.echo( h[ e ] ); // regexp ABC WScript.echo( h[ f ] ); // foo a WScript.echo( h[ g ] ); // foo b h[ 01 ] = "01"; WScript.echo( h[ 1 ] ); // 01 WScript.echo( a == b ); // false WScript.echo( a === b ); // false WScript.echo( a == a ); // true WScript.echo( a === a ); // true