返回信息流函数就是对象这点还是挺赞的
善于利用这点可以写出挺优雅的程序出来
如回调函数
[quote]function Foo() {
this.func = null;
this.bar = function() {
if (this.func != null) this.func();
};
}
function Bar() {
// do something
}
var foo = new Foo();
foo.func = Bar;
foo.bar();
[/quote]
又如自定义forEach
[quote]function forEach(list, func) {
// 枚举list中的的每一个元素
var elem = 其中一个元素
func(elem);
}
function printElem(elem) {
// do something
}
forEach(list, pringElem);
[/quote]
虽然C/C++也能用类似的方法,不过javascript能做到这点还是值得赞的
这是一条镜像帖。来源:北邮人论坛 / soft-design / #22290同步于 2007/11/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
最近发现javascript也没有想象中那么烂
Quake
2007/11/15镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
还有一个,函数的扩展
[quote]function Foo () {
this.func = funciton () {
// do something
};
}
var foo = new Foo();
var oldFunc = foo.func;
foo.func = function () {
oldFunc.call(this);
// do other things
}
[/quote]
package elvismap.component{
import elvismap.data.*;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import fl.controls.ComboBox;
import fl.controls.CheckBox;
import fl.containers.UILoader;
import flash.net.URLRequest;
import fl.data.DataProvider;
public class Map extends MovieClip {
private var _fdcName:String;
private var _fdcPath:String;
private var _loader:UILoader;
public function Map() {
initComponents();
//this.loader.addEventListener(ProgressEvent.PROGRESS,loadProgress);
this.loader.addEventListener(Event.COMPLETE,this.loadComplete);
}
//更新界面
public function updateMap(xml:XML):void {
this.fdcName = xml..Map.@fdcName;
this.fdcPath = xml..Map.@fdcPath;
//this.loader.load(new URLRequest(Config.RESOURCE_PATH+this.fdcPath));
this.loader.load(new URLRequest(DataOperator.RESOURCE_PATH+this.fdcPath));
addChild(loader);
}
//初始化控件
private function initComponents():void {
this.name = "MapPane";
this.loader = new UILoader;
}
//========================
//========事件方法=========
//========================
private function loadProgress(event:ProgressEvent):void {
//trace(String(event.target.percentLoaded));
}
private function loadComplete(event:Event):void {
var uiLdr:UILoader = event.currentTarget as UILoader;
uiLdr.width = uiLdr.content.width;
uiLdr.height = uiLdr.content.height;
}
//========================
//========辅助方法=========
//========================
//重写输出
public override function toString():String {
return "MapPane[fdcName=" + this.fdcName + ",fdcPath=" + this.fdcPath + "]";
}
public function set fdcName(fdcName:String):void {
this._fdcName=fdcName;
}
public function get fdcName():String {
return this._fdcName;
}
public function set fdcPath(fdcPath:String):void {
this._fdcPath=fdcPath;
}
public function get fdcPath():String {
return this._fdcPath;
}
public function set loader(loader:UILoader):void {
this._loader=loader;
}
public function get loader():UILoader {
return this._loader;
}
}
}