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 相关文章推荐
Javascript基础教程之if条件语句
Jan 18 Javascript
jQuery实现tag便签去重效果的方法
Jan 20 Javascript
JS实现图片高亮展示效果实例
Nov 24 Javascript
AngularJS实现表单手动验证和表单自动验证
Dec 09 Javascript
使用jquery.form.js实现图片上传的方法
May 05 Javascript
javascript面向对象程序设计高级特性经典教程(值得收藏)
May 19 Javascript
你不知道的Vue技巧之--开发一个可以通过方法调用的组件(推荐)
Apr 15 Javascript
JS实现在线ps功能详解
Jul 31 Javascript
微信小程序 SOTER 生物认证DEMO 指纹识别功能
Dec 13 Javascript
JS字符串和数组如何实现相互转化
Jul 02 Javascript
vue中destroyed方法的使用说明
Jul 21 Javascript
Vue 3自定义指令开发的相关总结
Jan 29 Vue.js
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
php中时间轴开发(刚刚、5分钟前、昨天10:23等)
2011/10/03 PHP
浅谈php7的重大新特性
2015/10/23 PHP
初识JQuery 实例一(first)
2011/03/16 Javascript
javascript window.confirm确认 取消对话框实现代码小结
2012/10/21 Javascript
将字符串转换成gb2312或者utf-8编码的参数(js版)
2013/04/10 Javascript
Javascript中3种实现继承的方法和代码实例
2014/08/12 Javascript
$(&quot;&quot;).click与onclick的区别示例介绍
2014/09/25 Javascript
JavaScript基础函数整理汇总
2015/01/30 Javascript
Bootstrap每天必学之按钮(一)
2015/11/24 Javascript
微信开发 使用picker封装省市区三级联动模板
2016/10/28 Javascript
Vue表单验证插件Vue Validator使用方法详解
2017/04/07 Javascript
微信小程序 支付功能实现PHP实例详解
2017/05/12 Javascript
Angular2关于@angular/cli默认端口号配置的问题
2017/07/15 Javascript
vue :src 文件路径错误问题的解决方法
2018/05/15 Javascript
vue和webpack项目构建过程常用的npm命令详解
2018/06/15 Javascript
angularjs 的数据绑定实现原理
2018/07/02 Javascript
基于vue实现圆形菜单栏组件
2019/07/05 Javascript
vue-cli3+typescript新建一个项目的思路分析
2019/08/06 Javascript
JavaScript 实现轮播图特效的示例
2020/11/05 Javascript
Python实现快速排序和插入排序算法及自定义排序的示例
2016/02/16 Python
python验证码识别教程之滑动验证码
2018/06/04 Python
python 3.3 下载固定链接文件并保存的方法
2018/12/18 Python
使用Python和Scribus创建一个RGB立方体的方法
2019/07/17 Python
django中使用Celery 布式任务队列过程详解
2019/07/29 Python
计算机应用专业毕业生求职信
2013/10/24 职场文书
单位创先争优活动方案
2014/01/26 职场文书
工程开工庆典邀请函
2014/02/01 职场文书
领导班子遵守党的政治纪律情况对照检查材料
2014/09/26 职场文书
公司放假通知怎么写
2015/04/15 职场文书
四大名著读书笔记
2015/06/25 职场文书
60条职场经典语录,总有一条能触动你的心
2019/08/21 职场文书
推荐六本经典文学奖书籍:此生必读
2019/08/22 职场文书
八年级作文之友谊
2019/12/02 职场文书
SQL Server基本使用和简单的CRUD操作
2021/04/05 SQL Server
MySQL基于索引的压力测试的实现
2021/11/07 MySQL
SQL Server内存机制浅探
2022/04/06 SQL Server