摘要: 闭包的应用闭包是ECMAScript最强大的特性之一。所谓“闭包”,是指在一个函数的执行过程中返回另一个函数对象。如:function testClosure(){ //private var a="private"; function getValue() { //引用testClosure的变量 alert( a ); } //返回对内部函数的引用 return getValue;}var getV = testClosure();getV();在阐述闭包执行调用的过程之前,我们需要了解一下JavaScript的垃圾自动回收机制。ECMA262规范要求遵循本规范的语言均 阅读全文
posted @ 2009-03-13 14:58 我的javaIT 阅读(136) 评论(0) 推荐(0) 编辑
摘要: 元编程的应用下面通过几个实例来帮助理解元编程思想。(1)根据现有的对象构造生成新的构造方法,使新对象构造的实例比原对象构造实例拥有更丰富的表现力。如:function createConstructor(fn){ var p= fn.prototype; function func(){} //保持原有fn的prototype chain func.prototype = p; //保持对象构造信息的正确性 func.prototype.constructor = p. constructor; //定义新对象构造 func.prototype.a="a"; func.pr 阅读全文
posted @ 2009-03-13 09:02 我的javaIT 阅读(198) 评论(0) 推荐(0) 编辑