环境:
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()
标签:
Prototype,对象

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“Prototype 学习 Prototype对象”

暂无“Prototype 学习 Prototype对象”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。