
2008年11月18日
很多javascript新手都觉得javascript的类、委托、事件很神秘,当你看下面几段代码后,你会觉得它也不过如此,和其他的面向对象的语言没什么差别。
javascript中的类:

javascript中的类
function Person(name, age) {
this._name = name;
this._age = age;
//对应Name的Get,Set方法,这个和Java的属性写法很像。
this.getName = function() {
return this._name
};
this.setName = function(name) {
this._name = name;
}
//对应Age的Get,Set方法
this.getAge = function() {
return this._age;
}
this.setAge = function(age) {
this._age = age;
}
//显示Person的信息方法
this.show = function() {
alert("Name:" + this.getName() + "; Age:" + this.getAge());
}
}
//空构造方法
var p1 = new Person();
p1.setName("Southsea");
p1.setAge(23);
p1.show();
//带参的构造方法
var p2 = new Person("Southsea", 23);
p2.show();
//注:Javascript中没有真正的方法重载
看起来很简单吧。
下面我们把Pererson类的show方法加一个参数,让它具有委托的功能。

委托
function Person(name, age) {
this._name = name;
this._age = age;
//对应Name的Get,Set方法,这个和Java的属性写法很像。
this.getName = function() {
return this._name
};
this.setName = function(name) {
this._name = name;
}
//对应Age的Get,Set方法
this.getAge = function() {
return this._age;
}
this.setAge = function(age) {
this._age = age;
}
//显示Person的信息方法
this.show = function(delegate) {
if (delegate) {
delegate(this);
}
}//只有这段与上面的不同。
}
//订阅Person类的show
function showPerson(p) {
alert("Name:" + p.getName() + "; Age:" + p.getAge());
}
var p = new Person("Southsea", 23);
p.show(showPerson); //别写成p.show(showPerson());哦
javascript中的事件

事件
function Person(name, age) {
this._name = name;
this._age = age;
//对应Name的Get,Set方法,这个和Java的属性写法很像。
this.getName = function() {
return this._name
};
this.setName = function(name) {
this._name = name;
}
//对应Age的Get,Set方法
this.getAge = function() {
return this._age;
}
this.setAge = function(age) {
this._age = age;
}
this.onShow = null;//加了onshow事件
//显示Person的信息方法
this.show = function() {
if (this.onShow) {
this.onShow(this);
}
}
}
//订阅Person类的show
function showPerson(p) {
alert("Name:" + p.getName() + "; Age:" + p.getAge());
}
var p = new Person("Southsea", 23);
p.onShow = showPerson; //千万别写成p.onShow = showPerson();
p.show();
委托和事件都看起来很简单吧。
javascript的动态类,它的格式是与JSON一样的。

动态类
var person = {
"Name": "Southsea",
"Age": 23, "show": function() {
alert("Name:" + person.Name + "; Age:" + person.Age);
}
};
person.show();
上面的几段代码是不是看起来很简单呀,希望这编文章对你有一定的帮助。。。
posted @ 2008-11-18 12:59 SouthSea 阅读(523) 评论(5)
编辑
今天在Terry Lee的博客中看到《Silverlight 3 -瞥》,之后找到Scott的原文《Update on Silverlight 2 - and a glimpse of Silverlight 3》,发现在Scott的文章里提到了K2的Blackpoint。
Silverlight已经发布了一段时间,但很少听说有企业用Silverlight来做产品或者做企业级开发,K2在很早前已经着手Silverlight版的设计工具"Blackpoint"(如果没记错的话,应该在Silverlight 2 Beta 1刚来出来的时候就开始开发Silverlight版的流程设计工具),现在blackpoint 已经是beta 2了,应该在不久的将来会发布正式版,期待中。。。
K2 Blackpoint与MOSS完美结合,它真正能做到No Code的流程开发。它和K2[blackpearl]差别很大。
了解K2的同学都知道K2[blackpearl]是一个很强大的流程平台,其中的"K2 Designer for Visual Studio"是个很强大的流程设计工具,但使用它来设计流程,流程设计者需要具备有编码能力。
而blackpoint并不需要编码,只要流程设计人员懂业务,就能完成企业流程的设计,的确很方便。看起来也非常好看。

本人前段时间在ASUS的项目中也写了一个Silverlight的控件,此控件为了完善K2本身的Viewflow而写的,这个控件的原型是我的老大David Dong(台湾的MVP)写的,当时他是基于Silverlight 1来写的,我后来把它完善,此控件在很方便的查看流程的当前节点的审批人员的相关信息,比K2本身自带的Viewflow多了这个功能(这可是很多企业都需要的哦,特别是在中国的企业)。现在Show出来给大家看看吧,样子不是很好看(这是我们的Beta 1,呵呵 。。。有待改善)。

1.能拖拽 2.能放大缩小
现在在华在地区已经有很几家企业打算使用这个控件了,(自我感觉良好,呵呵。。)我现在已经着手把它改成Silverlight 2,但由于 Silverlight 1 和 2差别很大,还有些难点要克服。
posted @ 2008-11-18 11:09 SouthSea 阅读(3035) 评论(11)
编辑