Prototype Learning Prototype object

Source: Internet
Author: User
Tags prototype definition

Environment:
Prototype Version: '1. 6.1 _ rc3'
Aptana Studio, build: 1.2.5.023247
IE7
FF2.0.0.4
Opera 10 beta
Copy codeThe Code is as follows:
Var Prototype = {
Version: '1. 6.1 _ rc3 ',
// Define the browser object
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 ('k') =-1,
MobileSafari:/Apple. * Mobile. * Safari/. test (ua)
}
})(),
// Define the browser Feature object
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 objects are returned by calling anonymous functions and immediately executing them. There are three methods to execute anonymous functions:
1. (function () {return 1}) () // () can be forcibly evaluated. return the function object and then execute the function.
2. (function () {return 1} () // return the result of function execution
3. void function () {alert (1)} () // void can also be forcibly calculated.
The method used to determine Opera isOpera uses window. opera. An object is returned in the Opera browser, and undefined is returned in other browsers.
BrowserFeatures objects mainly determine some features of the browser. FF supports many features that are not supported by IE. For example, the document. evalute method can operate HTML documents through XPATH, but IE does not.
The detailed usage of this function is as follows:
Copy codeThe Code is as follows:
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 shocould 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.
Here, _ proto _ can get the prototype object of the object under FF, that is, the prototype of the object. This is also the basis of the javascript Inheritance Mechanism. Prototype-based inheritance is not like class-based inheritance in C ++, JAVA, and C. There is also a metaclass inheritance method, which is often used in ruby and python.
ScriptFragment defines the regular expression of the script referenced in the webpage.
JSONFilter: the original prototype definition is used to better understand its usage --
Copy codeThe Code is as follows:
/* String # evalJSON internally callstring # 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 shoshould always set security comment delimiters (/*-secure-\ n... */) und sensitive JSON or JavaScript data to prevent Hijacking. (See this PDF document for more details .) */

Prototype. K is the method for returning the first parameter:
Copy codeThe Code is as follows:
Prototype. K ('Hello world! '); //-> 'Hello world! '
Prototype. K (1.5); ///-> 1.5
Prototype. K (Prototype. K); //-> Prototype. K

Describes the static and instance methods in JavaScript.
The static method should be expanded like this:
Date. toArray = function (){}
The toArray method is the static method of Date. It cannot be called (new Date (). toArray (); otherwise, an exception is thrown.
Use: Date. toArray ()
The instance method should be extended as follows:
Date. prototype. toArray2 = function (){}
The toArray2 method is the Date instance method, so you cannot call Date. toArray2 ();
Use: (new Date (). toArray2 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.