あなたはこれを行うことができますが、あなたは少し卑劣でなければなりません。私が理解しているのは、ホームベイクされたオブジェクトをSessionに格納すると、すべてのデータが保持されますが、すべての機能が失われるということです。機能を取り戻すには、セッションからデータを「再水和」する必要があります。
JScriptforASPの例を次に示します。
<%@ Language="Javascript" %>
<%
function User( name, age, home_town ) {
// Properties - All of these are saved in the Session
this.name = name || "Unknown";
this.age = age || 0;
this.home_town = home_town || "Huddersfield";
// The session we shall store this object in, setting it here
// means you can only store one User Object per session though
// but it proves the point
this.session_key = "MySessionKey";
// Methods - None of these will be available if you pull it straight out of the session
// Hydrate the data by sucking it back into this instance of the object
this.Load = function() {
var sessionObj = Session( this.session_key );
this.name = sessionObj.name;
this.age = sessionObj.age;
this.home_town = sessionObj.home_town;
}
// Stash the object (well its data) back into session
this.Save = function() {
Session( this.session_key ) = this;
},
this.Render = function() {
%>
<ul>
<li>name: <%= this.name %></li>
<li>age: <%= this.age %></li>
<li>home_town: <%= this.home_town %></li>
</ul>
<%
}
}
var me = new User( "Pete", "32", "Huddersfield" );
me.Save();
me.Render();
// Throw it away, its data now only exists in Session
me = null;
// Prove it, we still have access to the data!
Response.Write( "<h1>" + Session( "MySessionKey" ).name + "</h1>" );
// But not its methods/functions
// Session( "MySessionKey" ).Render(); << Would throw an error!
me = new User();
me.Load(); // Load the last saved state for this user
me.Render();
%>
セッションへの保存状態を管理する非常に強力な方法であり、必要に応じてDB呼び出し/XMLなどに簡単に切り替えることができます。
アンソニーがスレッドについて提起していることは興味深いです。彼の知識の深さを知っているので、それは正しいと思いますが、小さなサイトであればそれを回避できるのであれば、中規模のサイトでこれを使用しました( 1日1万人の訪問者)何年もの間、実際の問題はありません。