2008年1月3日 星期四

JavaScript Class Define And Enumerating or Printing Properties


//create a class
function Person(name, age) {
this.name = name || "";
this.age = age || 0;
}
Person.prototype = new Person;
var john = new Person("John", 29);// add some custom properties
john.language = "English";
john.wife = "Mary";//Enumerate using the standard forEach method:

for(prop in john)
{
str+=prop + " value :"+ john[prop]+"\n";//Concate prop and its value from object
}
alert(str);


or


var obj = objFrm.elements["name"];

var str=""; //variable which will hold property values
for(prop in obj)
{
alert(prop);
str+=prop + " value :"+ obj[prop]+"\n";//Concate prop and its value from object
}
alert(str); //Show all properties and its value

沒有留言:

關於我自己

Aspire freedom , Hope to do Soming make self complete ~