Prototype 学习 Prototype对象


Posted in Javascript onJuly 12, 2009

环境:
Prototype Version: '1.6.1_rc3'
Aptana Studio, build: 1.2.5.023247
IE7
FF2.0.0.4
Opera 10 beta

var Prototype = { 
Version: '1.6.1_rc3', 
//定义浏览器对象 
Browser: (function(){ 
var ua = navigator.userAgent; 
var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'; 
return { 
IE: !!window.attachEvent && !isOpera, 
Opera: isOpera, 
WebKit: ua.indexOf('AppleWebKit/') > -1, 
Gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, 
MobileSafari: /Apple.*Mobile.*Safari/.test(ua) 
} 
})(), 
//定义浏览器Feature对象 
BrowserFeatures: { 
XPath: !!document.evaluate, 
SelectorsAPI: !!document.querySelector, 
ElementExtensions: (function() { 
var constructor = window.Element || window.HTMLElement; 
return !!(constructor && constructor.prototype); 
})(), 
SpecificElementExtensions: (function() { 
if (typeof window.HTMLDivElement !== 'undefined') 
return true; 
var div = document.createElement('div'); 
var form = document.createElement('form'); 
var isSupported = false; 
if (div['__proto__'] && (div['__proto__'] !== form['__proto__'])) { 
isSupported = true; 
} 
div = form = null; 
return isSupported; 
})() 
}, 
ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>', 
JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, 
emptyFunction: function() { }, 
K: function(x) { return x } 
}; 
if (Prototype.Browser.MobileSafari) 
Prototype.BrowserFeatures.SpecificElementExtensions = false;

Broswer对象是通过调用匿名函数并且立即执行的方式返回的,执行匿名函数的方法有三种:
1. (function(){return 1})() //()可以强制求值,返回函数对象然后执行函数
2. (function(){return 1}()) //返回函数执行的结果
3. void function(){alert(1)}() //void 也有强制运算的用法
其中判断Opera的方法isOpera用到了window.opera,在Opera浏览器中会返回一个对象,其它浏览器返回undefined
BrowserFeatures对象主要判断浏览器的一些特性,FF支持许多的特性在IE下不支持,比如document.evalute方法就可以通过XPATH的方式操作HTML文档,但IE就不支持。
此函数详细用法如下:
var xpathResult = document.evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result);

The evaluate function takes a total of five arguments:
xpathExpression: A string containing an xpath expression to be evaluated
contextNode: A node in the document against which the Xpath expression should be evaluated
namespaceResolver: A function that takes a string containing a namespace prefix from the xpathExpression and returns a string containing the URI to which that prefix corresponds. This enables conversion between the prefixes used in the XPath expressions and the (possibly different) prefixes used in the document
resultType: A numeric constant indicating the type of result that is returned. These constants are avaliable in the global XPathResult object and are defined in the relevaant section of the XPath Spec. For most purposes it's OK to pass in XPathResult.ANY_TYPE which will cause the results of the Xpath expression to be returned as the most natural type
result:An existing XPathResult to use for the results. Passing null causes a new XPathResult to be created.
其中__proto__这个在FF下可以取得对象的prototype对象,即对象的原型。这也是javascript继承机制的基础,基于原型的继承,不像通常的C++,JAVA,C#语言的基于类的继承。还有一种metaclass的继承方式,在ruby和python中常有应用。
其中ScriptFragment定义网页中引用脚本的正则表达式
JSONFilter:还是引用prototype原文的解释更清楚他的用法——
/*String#evalJSON internally calls String#unfilterJSON and automatically removes optional security comment delimiters (defined in Prototype.JSONFilter).*/ person = '/*-secure-\n{"name": "Violet", "occupation": "character"}\n*/'.evalJSON() person.name; //-> "Violet" 
/*You should always set security comment delimiters (/*-secure-\n...*/) around sensitive JSON or JavaScript data to prevent Hijacking. (See this PDF document for more details.)*/

Prototype.K就是返回第一个参数的方法:
Prototype.K('hello world!'); // -> 'hello world!' 
Prototype.K(1.5); // -> 1.5 
Prototype.K(Prototype.K); // -> Prototype.K

说明一下JavaScript里面的静态方法和实例方法
静态方法要这样扩展:
Date.toArray=function(){}
那么toArray方法就是Date的静态方法,不能这样调用(new Date()).toArray();否则会抛出异常
要这样用:Date.toArray()
实例方法要这样扩展:
Date.prototype.toArray2=function(){}
那么toArray2方法就是Date的实例方法了,不能这样调用Date.toArray2();
要这样用:(new Date()).toArray2()
Javascript 相关文章推荐
浏览器无法运行JAVA脚本的解决方法
Jan 09 Javascript
javascript 运算数的求值顺序
Aug 23 Javascript
jQuery中data()方法用法实例
Dec 27 Javascript
Bootstrap每天必学之缩略图与警示窗
Nov 29 Javascript
AngularJS基础 ng-keydown 指令简单示例
Aug 02 Javascript
jQuery EasyUI常用数据验证汇总
Sep 18 Javascript
JavaScript实现跟随滚动缓冲运动广告框
Jul 15 Javascript
前端axios下载excel文件(二进制)的处理方法
Jul 31 Javascript
微信小程序实现轨迹回放的示例代码
Dec 13 Javascript
VueCli4项目配置反向代理proxy的方法步骤
May 17 Javascript
vue实践---vue不依赖外部资源实现简单多语操作
Sep 21 Javascript
JS中如何优雅的使用async await详解
Oct 05 Javascript
javascript 动态加载 css 方法总结
Jul 11 #Javascript
checkbox 复选框不能为空
Jul 11 #Javascript
javascript 页面只自动刷新一次
Jul 10 #Javascript
javascript div 遮罩层封锁整个页面
Jul 10 #Javascript
JQuery 实现的页面滚动时浮动窗口控件
Jul 10 #Javascript
javascript 读取xml,写入xml 实现代码
Jul 10 #Javascript
jquery 1.3.2 IE8中的一点点的小问题解决方法
Jul 10 #Javascript
You might like
详解WordPress开发中get_header()获取头部函数的用法
2016/01/08 PHP
php构造函数与析构函数
2016/04/23 PHP
PHP用函数嵌入网站访问量计数器
2017/10/27 PHP
浅谈laravel-admin的sortable和orderby使用问题
2019/10/03 PHP
Thinkphp 框架扩展之Widget扩展实现方法分析
2020/04/23 PHP
Auntion-TableSort国人写的一个javascript表格排序的东西
2007/11/12 Javascript
jquery.ui.draggable中文文档
2009/11/24 Javascript
jquery 表单取值常用代码
2009/12/22 Javascript
JS调用CS里的带参方法实例
2013/08/01 Javascript
jQuery教程 $()包装函数来实现数组元素分页效果
2013/08/13 Javascript
JS实现拖动示例代码
2013/11/01 Javascript
JavaScript原生对象之String对象的属性和方法详解
2015/03/13 Javascript
浅谈Javascript中substr和substring的区别
2015/09/30 Javascript
JQuery 进入页面默认给已赋值的复选框打钩
2017/03/23 jQuery
jQuery简单绑定单个事件的方法示例
2017/06/10 jQuery
AngularJs中$cookies简单用法分析
2019/05/30 Javascript
如何阻止小程序遮罩层下方图层滚动
2019/09/05 Javascript
vue按需加载实例详解
2019/09/06 Javascript
解决VUE自定义拖拽指令时 onmouseup 与 click事件冲突问题
2020/07/24 Javascript
vue的$http的get请求要加上params操作
2020/11/12 Javascript
[44:01]2018DOTA2亚洲邀请赛3月30日 小组赛B组 EG VS paiN
2018/03/31 DOTA
探究数组排序提升Python程序的循环的运行效率的原因
2015/04/01 Python
python爬虫的工作原理
2017/03/05 Python
Python中index()和seek()的用法(详解)
2017/04/27 Python
Python字典操作详细介绍及字典内建方法分享
2018/01/04 Python
Python代码打开本地.mp4格式文件的方法
2019/01/03 Python
Numpy 中的矩阵求逆实例
2019/08/26 Python
Python 异常处理Ⅳ过程图解
2019/10/18 Python
JAVA及PYTHON质数计算代码对比解析
2020/06/10 Python
树莓派4B安装Tensorflow的方法步骤
2020/07/16 Python
python生成word合同的实例方法
2021/01/12 Python
Watch Station官方网站:世界一流的手表和智能手表
2020/01/05 全球购物
全神贯注教学反思
2014/02/03 职场文书
《太阳》教学反思
2014/02/21 职场文书
工业设计专业自荐书
2014/06/05 职场文书
大学学生会竞选稿
2015/11/19 职场文书