js框架中类继承的比较

2010-07-13 09:24:16.0

首先我们来看下jQuery之父john resig的实现方法:

jq之父写的类继承方式的特点是:

  • 有私有变量
  • 可以写在一个构造函数中
  • 可以多级继承,可以有多个父类(需要某种特定格式)
  • 继承基于prototype,且不会执行父类的构造函数
  • 保持了父类的函数链,可以在子类中用this._super简单调用
  • instanceof 对子类、父类都生效

这是目前为止我看到的最好的方法。除了定义了一个全局变量Class外

// Inspired by base2 and Prototype
(function(){
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function(){};
// Create a new Class that inherits from this class
Class.extend = function(prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
})();

转载:JavaScript框架比较–选择器(一)

2010-06-08 16:55:47.0

摘要:现代网站和web应用程序趋向于依赖客户端的大量的javascript来提供丰富的交互。特别是通过不刷新页面的异步请求来返回数据或从服务器端的脚本(或数据系统)中得到响应。在这篇文章中,你将会了解到javascript框架如何更快、更方便的创建互动性强、响应快得网站和web应用程序。

导言:JavaScript是一种面向对象的脚本语言,一直以来用作Web浏览器应用程序客户端脚本接口的选择。JavaScript允许Web开发人员编程与网页上的对象的工作,为凭空操作这些对象提供了一个平台。当JavaScript最初推出时,它通常用来提供一些微不足道的功能,如时钟、在浏览器状态栏中滚动文本。另外一个常用特色就是“rollover link”,即当用户的鼠标滑过对象时,其文本的颜色或背景图片发生改变。然而,近年来Ajax为网络编程带来了全新的互动,JavaScript几经发展变得更加有用。在Ajax之前,任何服务器端处理或数据库访问都需要整页面被“刷新”或由浏览器呈现新的页面。这不仅缓慢,令用户失望,而且也浪费了带宽和资源。

jquery异步加载我要啦流量统计

2010-05-27 08:41:04.0

上一次我在博客中写道过异步加载统计JS来提高网站性能

Google analytics虽然支持异步加载但是使用习惯和功能都不符合国情

所以我还是放弃使用Google analytics,国内有很多网站流量统计站点,用的最多的是cnzz和51la的

magento中使用jquery

2010-05-25 20:16:50.0
magento中使用jquery的最大阻碍就是$报错

其实只要在jquery.js或者jquery.min.js中加入这一段jquery防冲突的语法就OK了

//各个js库之间的主要冲突在于$的冲突,这个方法是用来处理这个问题的
jQuery.noConflict();
//原本使用jQuery代码部分的$用jQuery替代

异步加载广告,统计JS代码,来提高网站性能

2010-05-15 22:22:44.0

外部JS的阻塞下载

通常JavaScript文件的加载会占用网页中其他元素的下载资源,在一些特别追求Web应用响应速度而且JavaScript文件并不那么小的时候,可以考虑使用异步加载JavaScript的方法来避免影响正常内容的加载进程

    有人会问:为什么JS不能像CSS、image一样并行下载了?这里就不介绍了,有兴趣的同学可以去查看下这篇文章高性能web开发 - 如何加载JS,JS应该放在什么位置?