📄 cloneable.jsc
字号:
# language: jsvm2
/**
* @fileoverview js.lang.Cloneable class {@link http://jsvm.org/}
* @file Cloneable.jsc
* @author Wan Changhua * @version 2.01, 10/23/05 * @since JSVM2.0
*/
package js.lang;
import js.lang.JObject;
import js.lang.ArgumentException;
/** * A class extends the <code>js.lang.Cloneable</code> class to * indicate to the clone() method that it * is legal for that method to make a * field-for-field copy of instances of that class.
* @author Wan Changhua * @version 2.01, 10/23/05
* @extends JObject * @class js.lang.Cloneable is the base class for all can be cloned class.
* @constructor * @throws ArgumentException if param error * @return A new Cloneable
* @see JObject js.lang.JObject is the base class for this
*/
class Cloneable ()
{
}
/** * Creates and returns a copy of this object. The precise meaning * of "copy" may depend on the class of the object. The general * intent is that, for any object x, the expression: * The method clone for class Object performs a * specific cloning operation. * The class does not itself extends the class * Cloneable, so calling the clone method on an object * whose class is Object will result in throwing an * exception at run time. * * @exception ArgumentException if the object's class does not * support the <code>Cloneable</code> class. Subclasses * that override the <code>clone</code> method can also * throw this ArgumentException to indicate that an instance cannot * be cloned.
* @return a clone of this instance.
* @type Object */
Cloneable.prototype.clone = function ()
{
var co = this.getClass().newInstance();
for (var p in this)
{
var v = this[p];
if (typeof(v) == "function")
{
continue;
}
co[p] = v;
}
return co;
}
/** * Creates and returns a copy of this parameter.
* @param {Object} obj an object * @exception ArgumentException if the parameter can not been cloned.
* @return a clone of this parameter.
* @type Object */
Cloneable.cloneObject = function (o)
{
if (o instanceof Cloneable)
{
return o.clone();
}
var type = typeof(o);
switch (type)
{
case "undefined" :
case "boolean" :
case "string" :
case "number" : return o;
case "function" : return o; //TODO
case "object" :
if (o == null)
{
return null;
}
if (o instanceof Date)
{
return new Date(o.getTime());
}
if (o instanceof Number)
{
return new Number(o.valueOf());
}
if (o instanceof Boolean)
{
return new Boolean(o.valueOf());
}
if (o instanceof Error)
{
return new Error(o.number, o.message);
}
if (o instanceof Array)
{
return o.concat([]);
}
if (o instanceof JObject)
{
throw new ArgumentException(Cloneable.getName()
+ ".cloneObject(o) error, coundn't clone a '"
+ o.getClass().getName()
+ "' instance.");
}
if (o instanceof Object)
{
var co = {};
for (var k in o)
{
co[k] = o[k];
}
return co;
}
default : break;
}
throw new ArgumentException(Cloneable.getName()
+ ".cloneObject(o) error, [type: "
+ type + "]");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -