Js功能强大灵活,不仅可以用来做网页客户端,还可以写asp后台代码,在Windows中与WSH的环境中更是强大。
以下代码只是我自编自用的Js类framework的一部分,经常被我用来做自动化测试,网站前后台。
这是框架的主要部分,用来创建或扩展一个Js原形对象。
/* javascript prototype.
* Authors: perry < ppsoft268@gmail.com >
* Date : April 11, 2009
*/
if ( parseFloat(WSH.version) < 5.6 )
{
WSH.echo('this script needs WSH Version 5.6 or Later!');
WSH.Quit(1);
}
/**
* @class: MY
* @static Object
* @description: MY core utilities and functions.
*/
var MY = {
/**
* The version of the framework
* @type String
*/
version: '1.0.0',
versionDetail: {
major: 1,
minor: 0,
patch: 0},
Shell: emb1,
FileIO: emb2,
Network: emb3,
objectName: MY_NS_MY,
objectPath: MY_NS_MY,
toString: function() { return this.objectPath; }},
MY_OBJ_BASE = null,
MY_OBJ_MY = MY;
var MY_DIR_MY = MY.FileIO.GetParentFolderName(WSH.ScriptFullName),
MY_DIR_LOG = MY.FileIO.BuildPath(MY_DIR_MY, 'log'),
MY_DIR_APPLOG = MY.FileIO.BuildPath(MY_DIR_MY, 'log\\app'),
MY_DIR_TESTLOG = MY.FileIO.BuildPath(MY_DIR_MY, 'log\\test'),
MY_DIR_SCRIPTS = MY.FileIO.BuildPath(MY_DIR_MY, 'scripts');
var MY_FILE_APP = WSH.ScriptFullName,
MY_FILE_CONFIG = MY.FileIO.BuildPath(MY_DIR_MY, 'configure.xml');
/**
* @class: MY.ClassFactory
* @static Object
* @description:
*/
MY.ClassFactory = {
//public
create: function(c, n, h, e, r)
{
var o = null,
p = '';
if ( !n )
n = MY_OBJ_MY;
switch ( typeof n )
{
case 'string':
o = this.namespace(n);
break;
case 'object':
o = n;
break;
}
if ( typeof o != 'object' || !o )
return null;
if ( typeof o.hasOwnProperty != 'function' )
return null;
if ( !o.hasOwnProperty('objectPath') )
return null;
p = o.objectPath + '.' + c;
if ( typeof o[c] != 'function' )
{
o[c] = function()
{
this.objectName = c;
this.objectPath = p;
this.instanceOf = function(cls) { return (this instanceof cls); };
this.toString = function() { return this.objectPath; };
if ( typeof this.initialize == 'function' )
{
if ( typeof this.initialize.apply == 'function' )
{
this.initialize.apply(this, arguments);
}
else
{
var x = this,
g = arguments,
z = 'x.initialize(';
for ( var i = 0; i < g.length; z += 'g[' + i.toString() + ']', i++ )
if ( i > 0 )
z += ',';
eval(z + ')');
}
}
};
}
if ( typeof h == 'function' )
{
this.extend(o[c].prototype, h.prototype, true);
this.extend(o[c], h, true);
}
if ( typeof e == 'function' )
{
this.extend(o[c].prototype, e.prototype, r);
this.extend(o[c], e, r);
}
this.extend(o[c],
{
createInstance: function()
{
var x = null,
g = arguments,
z = 'x = new ' + p;
if ( g.length )
{
z += '(';
for ( var i = 0; i < g.length; z += 'g[' + i.toString() + ']', i++ )
if ( i > 0 )
z += ',';
z += ')';
}
eval(z);
return x;
},
extend: function(s, r)
{
return MY.ClassFactory.extend(o[c].prototype, s, r);
},
toString: function()
{
return p;
}
}, true);
if ( typeof h == 'object' && h )
o[c].extend(h, true);
if ( typeof e == 'object' && e )
o[c].extend(e, r);
return o[c];
},
//public
extend: function(d, s, r)
{
//Some functions need to override.
var a = ['toString'],
p;
if ( !s || !d )
return d;
for ( p in s )
a.push(p);
for ( var i in a )
{
p = a[i];
try
{
if ( (d[p] && !r) )
continue;
switch (typeof s[p])
{
case 'object':
if ( !s[p] )
throw s[p];
switch ( s[p].constructor )
{
case Array:
d[p] = new Array;
for ( var j = 0; j < s[p].length; j++ )
d[p].push(s[p][j]);
break;
case Object:
d[p] = new Object;
arguments.callee(d[p], s[p], true);
break;
case Date:
d[p] = new Date(s[p].getTime());
break;
default:
d[p] = s[p];
break;
}
break;
case 'boolean':
case 'number':
case 'string':
default: d[p] = s[p]; break;
}
}
catch (e)
{
d[p] = null;
}
}
return d;
},
//public
namespace: function(n)
{
if ( typeof n == 'object' )
return n;
if ( typeof n != 'string' )
return null;
var l = n.split('.'),
o = MY_OBJ_MY,
p = '';
for ( var i = 0; i < l.length; i++ )
{
if ( i )
p += '.';
p += l[i];
if ( l[i] == o.objectName )
if ( p == o.objectPath )
continue;
if ( typeof o[l[i]] != 'object' )
o[l[i]] = {
objectName: l[i],
objectPath: p,
toString: function()
{
return this.objectPath;
}
};
o = o[l[i]];
}
return o;
},
//public
isDefined: function(n)
{
if ( typeof n == 'object' )
return n != null;
if ( typeof n == 'function' )
return true;
if ( typeof n != 'string' )
return false;
if ( !/[\w\$\.]+/i.test(n) )
return false;
var o = null;
try { o = eval(n); }
catch (e) { return false; }
if ( !o )
return false;
if ( typeof o == 'object' )
return true;
return typeof o == 'function';
},
//public
bindOle: function(ns, id)
{
var x = id;
if ( typeof id == 'string' )
x = this.newOle(id);
if ( typeof x != 'object' || x == null )
return false;
if ( !this.namespace(ns) )
return false;
eval(ns + '=x;');
return true;
},
//public
newOle: function(id)
{
if ( typeof id != 'string' )
return null;
if ( /^\{?[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\}?$/i.test(id) )
try { return this.newOle(MY.Shell.RegRead('HKCR\\CLSID\\' + id + '\\ProgID\\')); }
catch (e){}
try { return new ActiveXObject(id); }
catch (e){}
return null;
},
//public
newXml: function()
{
var xmlProgIds = [
'Msxml2.DOMDocument',
'Microsoft.XMLDOM'],
o = null;
for ( var i = 0; i < xmlProgIds.length && !o; i++ )
o = this.newOle(xmlProgIds[i]);
return o;
}
};