SoFunction
Updated on 2025-04-14

Prototype Learning Prototype Objects

environment:
Prototype Version: '1.6.1_rc3'
Aptana Studio, build: 1.2.5.023247
IE7
FF2.0.0.4
Opera 10 beta
Copy the codeThe code is as follows:

var Prototype = {
Version: '1.6.1_rc3',
//Define browser object
Browser: (function(){
var ua = ;
var isOpera = () == '[object Opera]';
return {
IE: !! && !isOpera,
Opera: isOpera,
WebKit: ('AppleWebKit/') > -1,
Gecko: ('Gecko') > -1 && ('KHTML') === -1,
MobileSafari: /Apple.*Mobile.*Safari/.test(ua)
}
})(),
//Define the browser Feature object
BrowserFeatures: {
XPath: !!,
SelectorsAPI: !!,
ElementExtensions: (function() {
var constructor = || ;
return !!(constructor && );
})(),
SpecificElementExtensions: (function() {
if (typeof !== 'undefined')
return true;
var div = ('div');
var form = ('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 ()
= false;

Browser objects are returned by calling anonymous functions and executing them immediately. There are three ways to execute anonymous functions:
1. (function(){return 1})() //() can force evaluation, return the function object and execute the function
2. (function(){return 1}()) //Return the result of function execution
3. void function(){alert(1)}() //void also has the use of forced operations
The method to judge Opera isOpera is used. An object will be returned in the Opera browser, and other browsers will return undefined.
The BrowserFeatures object mainly judges some features of the browser. FF supports many features that are not supported in IE. For example, the method can operate HTML documents through XPATH, but IE does not support them.
The detailed usage of this function is as follows:
Copy the codeThe code is as follows:

var xpathResult = (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__, a prototype object of the object can be obtained under FF, that is, the prototype of the object. This is also the basis of the JavaScript inheritance mechanism, which is based on prototype inheritance, unlike the usual C++, JAVA, and C# languages. There is also a metaclass inheritance method, which is often used in ruby ​​and python.
Where ScriptFragment defines the regular expression of the reference script in the web page
JSONFilter: It is better to use it by quoting the original text of prototype.
Copy the codeThe code is as follows:

/*String#evalJSON internally calls String#unfilterJSON and automatically removes optional security comment delimiters (defined in ).*/

person = '/*-secure-\n{"name": "Violet", "occupation": "character"}\n*/'.evalJSON() ; //-> "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.)*/

It is the method to return the first parameter:
Copy the codeThe code is as follows:

('hello world!'); // -> 'hello world!'
(1.5); // -> 1.5
(); // ->

Explain the static methods and instance methods in JavaScript
The static method needs to be extended like this:
=function(){}
Then the toArray method is the static method of Date. You cannot call it like this (new Date()).toArray(); otherwise an exception will be thrown.
To use this: ()
The instance method needs to be extended like this:
.toArray2=function(){}
Then the toArray2 method is the instance method of Date.toArray2() cannot be called like this;
To use this: (new Date()).toArray2()