ZT - Simple JavaScript Object Reflection API (.NET Style)
来源:互联网 发布:装修设计什么软件 编辑:程序博客网 时间:2024/06/07 18:24
csdn 不能分享网址, 就放在这里吧
原始地址: http://pietschsoft.com/post/2008/02/Simple-JavaScript-Object-Reflection-API-%28NET-Style%29.aspx
I was thinking about how JavaScript JSON serializers go about serializing objects. But how does the serializer know about each of the objects properties? I figured JavaScript must have some method of object reflection (similar to .NET Reflection) and it does.
Here's a simple Reflection namespace that allows you to more easily reflect through an objects methods and properties:
if (typeof PietschSoft == "undefined") var PietschSoft = {};
if (typeof PietschSoft.Reflection == "undefined") PietschSoft.Reflection = {};
PietschSoft.Reflection.GetProperties = function(obj){
var props = new Array();
for (var s in obj)
{
if (typeof(obj[s]) != "function") {
props[props.length] = s;
}
}
return props;
};
PietschSoft.Reflection.GetMethods = function(obj){
var methods = new Array();
for (var s in obj)
{
if (typeof(obj[s]) == "function") {
methods[methods.length] = s;
}
}
return methods
};
And, here's some simple code using the above simple reflection api:
/// Define our Person Object
Person = function(){
this.FirstName = "";
this.LastName = "";
};
Person.prototype.TestFunction = function(){return "Test Function";};
// Define our instance of the Person object
var p = new Person();
p.FirstName = "Chris";
p.LastName = "Pietschmann";
/// Loop through the Objects Properties
var props = PietschSoft.Reflection.GetProperties(p);
for (var i in props)
{
alert(props[i] + " : " + p[props[i]]);
}
/// Loop through the Objects Methods
var methods = PietschSoft.Reflection.GetMethods(p);
for (var i in methods)
{
alert(methods[i] + " : " + p[methods[i]]);
}
- ZT - Simple JavaScript Object Reflection API (.NET Style)
- simple water without reflection
- Dapper - a simple object mapper for .Net
- JAVASCRIPT----style 中visibility和display之间的区别(ZT)
- How to pass javascript complex object to ASP.NET Web Api and MVC
- Java Reflection API学习
- The Reflection API
- Java Reflection API简介
- Java Reflection API Notes
- 使用反射(Reflection)API
- Java Reflection API简介
- PHP Reflection API详解
- PHP Reflection API详解
- PHP Reflection API
- Qt4中利用Meta Object实现类拟.NET和Java的反射(Reflection)功能
- Reflection in Javascript
- javascript style
- Simple way to implement caching in ASP.NET Web API
- MSDN 适用于 Visual Basic 6.0 用户的帮助
- 懒的精文
- 2010年企业网站营销
- 终于读完了《跟我一起写 Makefile》 !也算了解了一下Makefile!感谢逸学堂大侠的分享和作者的无私奉献!
- 工商银行 U盾 -100003 数据签名错误
- ZT - Simple JavaScript Object Reflection API (.NET Style)
- 五大开源游戏引擎介绍(转)
- 开通一个csdn的blog
- 第一次
- 金岩石精彩语录
- VC6调用VS2008的动态库
- Oracle中 Alter Table 语句的使用
- 什么是轨对轨
- 某基于DEDECMS5.5网站的安全检测初步报告