Prototype 学习 Prototype对象

(编辑:jimmy 日期: 2024/10/10 浏览:2)

环境:
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()
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。