Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

addProto.as

Name: addProto() - Add prototype: adds prototype to a class via an instance
Author: senocular: www.senocular.com
Date: 1899-12-31T00:19:30.800
Documentation:
Object ADDPROTO: adds a prototype to a class through
an instance of that class.
 
Arguments:
- name: name of the new property or method to be added.
- value: the actual property or method to be added.

* @example
function ObjClass() {
	this.name = "ObjClassInstance";
}
testMe = function () {
	trace("test function called in "+ this.name);
}
inst = new ObjClass();
inst.test();
trace("^call one");
inst.addProto("test", testMe);
inst.test();
trace("^call two");
// outputs:
^call one
test function called in ObjClassInstance
^call two

// the first call never occurs because test wasn't
// at that point defined for inst.  inst.addProto("test", testMe);
// is equivalent to ObjClass.prototype.test = testMe;

1
2
3
4
5
6
7
8
9
// through constructor
Object.prototype.addProto = function(name, value){
	this.constructor.prototype[name] = value;
}
// or
// through __proto__
Object.prototype.addProto = function(name, value){
	this.__proto__[name] = value;
}