代码改变世界

随笔档案-2012年7月9日

ECMAScript 继承机制实现

2012-07-09 17:26 by @影子@, 173 阅读, 收藏,
摘要: 今天应该花时间研究的文章Safe Factory Pattern - Private instance state in JavaScripthttp://www.codeproject.com/Articles/133118/Safe-Factory-Pattern-Private-instance-state-in-JavECMAScript 继承机制实现http://www.w3school... 阅读全文

有用的工具函数

2012-07-09 16:41 by @影子@, 197 阅读, 收藏,
摘要: 本文摘自《Javascript权威指南》8.7节Page146对象工具函数var obj = {}; // <=> new Object(); // Add 'prop1'obj.prop1 = 1; // Add 'prop2'obj.prop2 = 2; // Delete 'prop1'delete obj.prop1; // Change value of 'prop2'obj.prop2 = 3;//Return a array that holds the names of the enumerable p 阅读全文

Javascript闭包模拟私有成员

2012-07-09 15:53 by @影子@, 330 阅读, 收藏,
摘要: 通过闭包可以使得外部原型方法无法访问到内部的成员。如下例所示,原型方法返回的是undefined,这是因为闭包存在于Immutable中,导致无法访问到getWidth和getHeight。function ImmutableRectangle(w,h){ this.getWidth = function () { return w; } ; this.getHeight = function () { return h; } ;}ImmutableRectangle.prototype.area = function(){ return this.getWidth()*this.getHei 阅读全文

this关键字

2012-07-09 14:02 by @影子@, 182 阅读, 收藏,
摘要: 在javascript中,必须为属性显式的指定this关键字 。 写成 return this.width*this.height如果觉得必须在每个实例字段前都使用this前缀很不好看,那么可以在自己的每个方法中使用with语句Rectangle.prototype.area = function() { with(this){ return width*height; }}【参考文章】http://www.quirksmode.org/js/this.htmlhttp://www.cnblogs.com/georgewing/archive/2009/09... 阅读全文