代码: |
/** * 定义一个全局对象, 属性 Version 在发布的时候会替换为当前版本号 */ var Prototype = { Version: '@@VERSION@@' } /** /** /** /** /** /** /** /*--------------------------------------------------------------------------*/ /** /*--------------------------------------------------------------------------*/ /** if (arguments.length == 1) |
代码: |
/** * 定义 Ajax 对象, 静态方法 getTransport 方法返回一个 XMLHttp 对象 */ var Ajax = { getTransport: function() { return Try.these( function() {return new ActiveXObject('Msxml2.XMLHTTP')}, function() {return new ActiveXObject('Microsoft.XMLHTTP')}, function() {return new XMLHttpRequest()} ) || false; }, emptyFunction: function() {} } /** /** /** /** if (this.options.method == 'post') { /** if (this.onComplete) { |
代码: |
/** * 针对 页面元素对象 的工具类,提供一些简单静态方法 */ var Field = { /** * 清除参数引用对象的值 */ clear: function() { for (var i = 0; i < arguments.length; i++) $(arguments[i]).value = ''; }, /** /** /*--------------------------------------------------------------------------*/ /** for (tagName in Form.Element.Serializers) { /** /* /** /** textarea: function(element) { /** /*--------------------------------------------------------------------------*/ /** /*--------------------------------------------------------------------------*/ /** /** /** Form.Observer = Class.create(); |
代码: |
/** * 根据 class attribute 的名字得到对象数组,支持 multiple class * */ document.getElementsByClassName = function(className) { var children = document.getElementsByTagName('*') || document.all; var elements = new Array(); for (var i = 0; i < children.length; i++) { var child = children[i]; var classNames = child.className.split(' '); for (var j = 0; j < classNames.length; j++) { if (classNames[j] == className) { elements.push(child); break; } } } return elements; } /*--------------------------------------------------------------------------*/ /** hide: function() { show: function() { remove: function(element) { /** /*--------------------------------------------------------------------------*/ /** Abstract.Insertion.prototype = { /** /** Insertion.Before = Class.create(); Insertion.Top = Class.create(); Insertion.Bottom = Class.create(); Insertion.After = Class.create(); /** |
prototype 还有两个源码文件 effects.js compat.js 就不贴出来了。两者并不常用,effects.js 看example 做花哨的效果还不错,不过代码中没有太多新鲜的东西。
需要指出的就是
compat.js 中 Funcation.prototype.apply 的实现有两个错误(应该是拼写错误), 我分别贴出来,大家比较一下就清楚了。
代码: |
/* 这是包含错误的原版本 if (!Function.prototype.apply) { // Based on code from http://www.youngpup.net/ Function.prototype.apply = function(object, parameters) { var parameterStrings = new Array(); if (!object) object = window; if (!parameters) parameters = new Array(); for (var i = 0; i < parameters.length; i++) object.__apply__ = this; return result; if (!Function.prototype.apply) { for (var i = 0; i < parameters.length; i++) object.__apply__ = this; return result; |
接下来是我模仿着编写的一个 Effect 的一个子类,用来实现闪烁的效果。
代码: |
Effect.Blink = Class.create(); Effect.Blink.prototype = { initialize: function(element, frequency) { this.element = $(element); this.frequency = frequency?frequency:1000; this.element.effect_blink = this; this.blink(); }, blink: function() { |
使用也很简单, 调用 new Effect.Blink(elementId) 就好了。
通过对 prototype 源码的研究,我想我对javascript又有了一点新的体会,而最大的体会就是 《Ajax : A New Approach to Web Applications》文章最后作者对设计人员的建议: to forget what we think we know about the limitations of the Web, and begin to imagine a wider, richer range of possibilities.