with句イロイロ
その1
with(document){ write("こんにちは"); // こんにちはの表示 }
その2
o = {}; o.p = "こんにちは"; with(o){ var func = function(){ alert(p) }; } func(); // こんにちはの表示
その3
var Foo = {}; Foo.Foo = function(){ }; Foo.Foo.prototype = { foo : function(){ alert('foo') } }; Foo.Bar = function(){ }; Foo.Bar.prototype = { bar : function(){ alert('bar') } }; var Hoge = function(){}; with(Foo.Foo){ Hoge.prototype = prototype; } // fooが表示されるよ。 new Hoge().foo(); with(Foo.Bar){ Hoge.prototype = prototype; } // barが表示されるよ。 new Hoge().bar();
その4
ここまでくるとなんなんだか…。
var Foo = function(){ }; Foo.prototype.foo = function(){ alert("foo") }; var Bar = function(){ }; with({Hoge01:Bar,Hoge02:Foo}){ Hoge01.prototype = Hoge02.prototype; } // fooの表示 new Bar().foo(); alert(Hoge01) // undefined alert(Hoge02) // undefined