Home > Web Front-end > JS Tutorial > Prototype learning Prototype object_prototype

Prototype learning Prototype object_prototype

WBOY
Release: 2016-05-16 18:50:08
Original
850 people have browsed it

Environment:
Prototype Version: '1.6.1_rc3'
Aptana Studio, build: 1.2.5.023247
IE7
FF2.0.0.4
Opera 10 beta

Copy code The code is as follows:

var Prototype = {
Version: '1.6.1_rc3',
// Define 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('KHTML') === -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: ']*> ([\S\s]*?)',
JSONFilter: /^/*-secure-([sS]*)*/s*$/,
emptyFunction: function() { },
K: function(x) { return x }
};
if (Prototype.Browser.MobileSafari)
Prototype.BrowserFeatures.SpecificElementExtensions = false;

The Broswer object is returned by calling an anonymous function and executing it immediately. There are three ways to execute an anonymous function:
1. (function(){return 1})() //() can force evaluation, 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 is also available Usage of forced operation
Among them, the method of determining Opera is isOpera uses window.opera, which will return an object in the Opera browser, and other browsers return undefined
The BrowserFeatures object mainly determines some features of the browser, and FF supports many Features are not supported under IE. For example, the document.evalute method can operate HTML documents through XPATH, but IE does not support it.
The detailed usage of this function is as follows:
Copy code The 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 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.
Among them, __proto__ can obtain 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, unlike the usual class-based inheritance in C, JAVA, and C# languages. There is also a metaclass inheritance method, which is often used in ruby ​​and python.
The ScriptFragment defines the regular expression that references the script in the web page
JSONFilter: It is better to quote the original explanation of the prototype to make its usage clearer -
Copy code The code is as follows:

/*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 is to return the first One-parameter method:
Copy code The code is as follows:

Prototype.K('hello world!'); // -> 'hello world!'
Prototype.K(1.5); // -> 1.5
Prototype.K(Prototype.K); // -> Prototype. K

Explain the static methods and instance methods in JavaScript
The static method should be extended like this:
Date.toArray=function(){}
Then the toArray method is Date Static methods cannot be called like this (new Date()).toArray(); otherwise an exception will be thrown.
To be used like this: Date.toArray()
The instance method should be extended like this:
Date.prototype. toArray2=function(){}
Then the toArray2 method is the instance method of Date. Date.toArray2() cannot be called like this;
It should be used like this: (new Date()).toArray2()
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template