// First, from the overall and global perspective, jquery's source code is almost all in the following code: (function () {//...}) (window );
// The first bracket contains an anonymous function, and the second bracket indicates that the code in the first bracket is executed immediately. // First, understand that there is no namespace in Javascript. To ensure that your JavaScript Functions and objects do not conflict with other objects, a javascript technique is used here: all your JavaScript Functions and objects are defined in an anonymous function, ensuring the effective range of the defined functions and objects and playing a role in the namespace. How can someone else use this anonymous function? The following code shows it: (function () {var jquery = Window. jquery = Window. $ = function () {// alert (1) ;}}) (window); window. $ (); // This makes jquery, the most important object in the jquery library, a property of the window object, so that you can use document elsewhere (document is also an attribute of window) jquery is the same.
JS inheritance has five implementation methods:
The first method of inheritance: Object impersonating
The following three rows are used to append the parent attributes and methods to the child to implement inheritance.
Step 1: This. method is a temporary property and points to the object pointed to by the parent,
Step 2: execute this. Method to execute the object function pointed to by the parent.
Step 3: destroy the this. Method attribute, that is, child has all the attributes and methods of the parent.
function Parent(username){this.username=username;this.hello=function(){alert(this.username);}}function Child(username,password){this.methed=Parent;this.methed(username);delete this.methed;this.password=password;this.world=function(){alert(this.password);}}var parent = new Parent("zhangsan");var child = new Child("lisi","123456");parent.hello();//zhangsanchild.hello();//lisichild.world();//123456
Inherit the second method: Call () method
The call method is a method in the function class.
The value of the first parameter of the call method is assigned to this
The second parameter of the call method is assigned to the parameters accepted by the class (that is, the method) in turn.
Function parent (username) {This. username = username; this. hello = function () {alert (this. username) ;}} function Child (username, password) {// at this time, the first parameter value "this" is the this that child passed to the parent class (that is, the method, the second parameter username is assigned to the usernameparent of the parent class (that is, the method. call (this, username); this. password = password; this. world = function () {alert (this. password) ;}} var parent = new parent ("zhangsan"); var child = new child ("Lisi", "123456"); parent. hello (); // zhangsanchild. hello (); // lisichild. world (); // 123456
The third method of inheritance: Apply () method
The apply method accepts two parameters,
A. the first parameter is the same as the first parameter of the call method, that is, this parameter is assigned to the class (that is, the method ).
B. The second parameter is of the array type. Each element in the array is assigned to the parameters accepted by the class (that is, the method) in turn.
function Parent(username){this.username=username;this.hello=function(){alert(this.username);}}function Child(username,password){Parent.apply(this, new Array(username));this.password=password;this.world=function(){alert(this.password);}}var parent = new Parent("zhangsan");var child = new Child("lisi","123456");parent.hello();//zhangsanchild.hello();//lisichild.world();//123456
The fourth method of inheritance: The prototype method, that is, the subclass appends all the attributes and Methods appended to the parent class through prototype to the child through prototype, thus implementing inheritance.
Function parent () {} parent. prototype. hello = "hello"; parent. prototype. sayhello = function () {alert (this. hello);} function Child () {}// append all attributes and Methods appended by prototype to child in the parent, thus inheriting child. prototype = new parent (); child. prototype. world = "world"; child. prototype. sayworld = function () {alert (this. world);} var c = new child (); C. sayhello (); C. sayworld ();
Summary on April 9