Base. js contains the following content Class creation and inheritance:Class. create (): create a Class, such as person = Class. create () Object. extend (destination, source): copy the method attribute in source to destination (use for propertyin source). Note that in javascript, except the basic type (Number, Boolean) this type of copy is generally just a copy reference. destination and source still point to the same method or object attribute (function array object) in object-oriented programming, generally. create creates a new class. If this class inherits from a class, Object is generally used. extend (class. prototype, parentClass. prototype) or Object. extend (class. prototype, aparentClassInstance)
Extension of Object constructor:An Object is a constructor of other Object instances (var a = new Object (). It is also the parent class of all other classes and is directly extended to the Object (note that it is not an extension Object. prototype, extended Object. prototype is equivalent to adding an instance method) is equivalent to adding a static method Object for the Object class. inspect (object): Call the inspect (if defined) or toString method of the object, and return a string of the Object to represent the object. keys (object): returns an array composed of all attributes and method names of an Object, for example, object. keys (document. body) Object. values (object): returns an array consisting of all the attributes of an Object and the values of the method, for example, object. values (docuement) Object. clone (object): returns the clone version of an Object, which actually executes the object. the extent method copies the method attribute in the object to a new object, and then returns this object.
Function bonding:Two Methods of the Function object are defined: bind and bindAsEventListener. These two methods are two methods of a Function. For java and c # programmers, it may be surprising to see this, in their opinion, functions are just an organizational structure of program statements. ---> how can there be methods and extensions? This is also a very powerful function of javascript and other scripting languages compared with java. A function is also an object, and the function name is the name of this object, as long as you want, you can also use new Function (...) to define the function, so it is normal to define the method for the function. However, the main function of these two functions is to solve the reference problem of this in javascript object-oriented programming. In javasctipt, The this keywordAlwaysPoint to the object that calls the function or to the object specified by the call and apply method (You can google this knowledge yourself, the introduction to prototype in the following series assumes that you are familiar with the javascript language. If you are not familiar with prototype, you can find thisJavascript authoritative guide this bookTo understand this problem, you must first understand it.AlwaysThis is a special keyword that cannot be considered as a common variable name. The most common mistake is to use this in the call of the returned function, for example, return function () {this. aMethod ()}. When you call the returned anonymous method next time, the reference content of this is directed to the object that calls this function. Remember that this is a keyword, no closure is generated because it is not a variable nameNumber extension (note that num can also be regarded as an object. In fact, it is automatically packaged into a Number object when used ):ToColorPart: converts a number to a hexadecimal value that can be used to represent color. For example, 7. toColorPart () => "07", 28. toColorPart () => "1C" succ: returns num ++, but does not change the value of num itself. Actually, return this + 1 times: call a function in turn from 0 to this number, for example, function a (n) {docuement. write (n)}, 10. times (a) will display 012345678910. Note that the function is also an object and there is no substantive difference with other objects.
Try object:The Try object provides an interesting feature. Let's take a look at the following code: var Ajax = {getTransport: function () {return Try. these (function () {return new XMLHttpRequest ()}, function () {return new ActiveXObject ('msxml2. XMLHTTP ')}, function () {return new ActiveXObject ('Microsoft. XMLHTTP ')}) | false;} The Try object provides the these method, which accepts a list of parameters of the function type and then executes these functions in turn, when a function does not produce an error, stop the execution and return the value returned by the function.PeriodicalExecuter (periodic executor) objectThis object is a simple encapsulation of the setInterval method. var a = new PeriodicalExecuter (callback, frequency) // callback: the name of the function to be executed frequency: interval of each Execution. stop ()