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

 

senocular.com ActionScript Library

isNaP.as

Name: isNaP() - is Not a Prototype. Determines if a property is instance specific or from a prototype
Author: senocular: www.senocular.com
Date: 1899-12-31T00:25:00.700
Documentation:
Object ISNAP: determines if a specific property
or method of an object is specific to that object or
given to it through a prototype.
 
Arguments:
- prop: the property or method (as a string) of the object to be checked.
 
Returns:
- null: if the property/method doesnt exist
- 1: if the property/method is not from a prototype
- -1: if the property/method exists as a prototype, but the value is different within 
this instance (instance property/method overrides the prototype)
- 0: if the property/method is given to this through a prototype (or if it's value is
the same as the prototype's value)
 
Warning:
- the return of 0 can mean two things.  1) that the property/method is, in fact, given to the
object as a result of the prototype OR 2) the property/method is unique to that object but
has the same value as the prototype property/method of the same name.  If that property/method 
is unique to that object, it will override the property/method prototype of the same name.
However, if it's value is the same as the prototype value, isNaP will still return 0.

Example:
a = new Object();
a.one = 2;
a.two = 3;
Object.prototype.three = 4
 
for (props in a) trace(props +": "+ a[props]); // all properties in a
trace("==========");
for (props in a) if (a.isNaP(props)) trace(props +": "+ a[props]); // all non prototype properties in a
// outputs:
three: 4
isNaP: [type Function]
two: 3
one: 2
==========
two: 3
one: 2
 
// isNaP not only works on Object Objects, but any other kind of object as well
b = [];
Array.prototype.c = 1;
if (b.isNaP("c")) trace ("c is unique");
else trace("c is from a prototype");
// traces: "c is from a prototype"

1
2
3
4
5
6
7
Object.prototype.isNaP = function(prop){
	if (this[prop] != undefined){ // if this property exists
		if (this.__proto__[prop] == undefined){ return 1; // if the prototype doesnt exist
		}else if (this.__proto__[prop] != this[prop]){ return -1; // if the prototype is different from the property
		}else return 0; // if the prototype exists and is the same as the objects property of the same type
	}else return null; // if property doesnt exist
}