Return to Snippet

Revision: 578
at July 19, 2006 18:31 by rolandog


Updated Code
/*	Makes the properties you pass in an array publicly available
	to access with an index: foo.propertyNames[0];	*/
Object.prototype.makePublic=function(array){
	var j=0;this.propertyNames=[];this.propertyValues=[];
	for(var i=0;i<array.length;++i){
		if(this[array[i]]!==undefined){
			this.propertyNames.push(array[i]);
			this.propertyValues.push(this[array[i]]);
			++j;
		}
	}
	var r=j?j:false;
	return r;
};

/*	Example:
	//Creating some 'attributes' in an Object called 'a'.
var a={href:"http://example.com",rel:"me",title:"mypage"};

	//Making only the object's 'href' attribute available, and assigning the length to a variable.
var l=a.makePublic(['href']);

	//Alerting the first property name publicly available, along with its value.:
alert(a.propertyNames[0]+"= "+a.propertyValues[0]);
*/

Revision: 577
at July 19, 2006 12:27 by rolandog


Initial Code
/*	Makes the properties you pass in an array publicly available
	to access with an index: foo.propertyNames[0];	*/
Object.prototype.makePublic=function(array){
	var j=0;this.propertyNames=[];this.propertyValues=[];
	for(var i=0;i<array.length;++i){
		if(this[array[i]]!==undefined){
			this.propertyNames.push(array[i]);
			this.propertyValues.push(this[array[i]]);
			++j;
		}
	}
	var r=j?j+1:false;
	return r;
};

/*	Example:
	//Creating some 'attributes' in an Object called 'a'.
var a={href:"http://example.com",rel:"me",title:"mypage"};

	//Making only the object's 'href' attribute available, and assigning the length to a variable.
var l=a.makePublic(['href']);

	//Alerting the first property name publicly available, along with its value.:
alert(a.propertyNames[0]+"= "+a.propertyValues[0]);
*/

Initial URL
http://rolandog.com/archives/2006/07/18/konstructor

Initial Description
This function allows an object's property names to be read by using an index. The properties can be accessed like: someobject.propertyNames[0] or someobject.propertyValues[0]. But you first have to make the object's properties available by calling someobject.makePublic(['name1','name2','name3',...,'nameN']). The array in the makePublic function is an array of the names of the properties you'd like to make available. Perhaps you're only interested in ['foo','bar']. This is useful for situations where you don't explicitly know if an object carries all properties, but you have an array of all the possible properties available.

Initial Title
Public properties for Objects in Javascript

Initial Tags
javascript

Initial Language
JavaScript