当前位置: 首页 > 工具软件 > Prototype > 使用案例 >

Prototype 学习——Prototype对象

章安宜
2023-12-01

环境:

Prototype Version: '1.6.1_rc3'

 

Aptana Studio, build: 1.2.5.023247

 

IE7

FF2.0.0.4

Opera 10 beta

 

=============================================================

  

 

 

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:

  1. xpathExpression: A string containing an xpath expression to be evaluated
  2. contextNode: A node in the document against which the Xpath expression should be evaluated
  3. 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
  4. 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
  5. 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原文的解释更清楚他的用法——

 

Prototype.K就是返回第一个参数的方法:

 

======================================================

 

说明一下JavaScript里面的静态方法和实例方法

 

静态方法要这样扩展:

Date.toArray=function(){}

那么toArray方法就是Date的静态方法,不能这样调用(new Date()).toArray();否则会抛出异常

要这样用:Date.toArray()

 

实例方法要这样扩展:

Date.prototype.toArray2=function(){}

那么toArray2方法就是Date的实例方法了,不能这样调用Date.toArray2();

要这样用:(new Date()).toArray2()

 

 类似资料: