Classic record of the essence of JavaScript Language

Source: Internet
Author: User

The book avoids chicken ribs and scum, only discusses the essence of the Department, not about DOM and HTML, only focus on the language itself.

JavaScript is indeed a very elegant language. Use Code directly.

 

The following code is taken from the essence of JavaScript language. Most of the code shows the characteristics of a prototype-based weak language,

After debugging, it is slightly modified.

 

// Add a method to the type
Function. prototype. method = function (name, func ){
This. prototype [name] = func;
Return this;
};

// Instantiate a constructor
If (typeof Object. beget! = 'Function '){
Object. beget = function (o ){
Var F = function (){};
F. prototype = o;
Return new F ();
};
}

// Exception Handling
Try {
Document. writeln (document. body. childNodes );
}
Catch (e ){
Document. writeln (e. name + ':' + e. message );
}

// Closure
Var add_the_handlers = function (nodes ){
Var I;
For (I = 0; I <nodes. length; I + = 1 ){
Nodes [I]. onclick = function (I ){
Return function (e ){
Alert (I );
};
} (I );
}
};
Add_the_handlers (document. body. childNodes );

// Module
String. method ('entityify ', function (){
Var entity = {
Quot :'"',
Lt: '<',
Gt: '>'
};
Return function (){
Return this. replace (/& ([^ &;] +);/g,
Function (a, B ){
Var r = entity [B];
Return typeof r === 'string '? R:;
}
);
};
}()
);
Document. writeln ('<br> ');
Document. writeln ('& lt; & quot; & gt;'. deentityify ());
Document. writeln ('20140901'. deentityify ());

// Pseudo class
Function. method ('new', function (){
Var that = Object. beget (this. prototype );
Var other = this. apply (that, arguments );
Return (typeof other = 'object' & other) | that;
});

Var Mammal = function (name ){
This. name = name;
This. age = 1;
};
Mammal. prototype. get_name = function (){
Return this. name;
};
Mammal. prototype. says = function (){
Return this. saying | '';
};
Mammal. prototype. get_age = function (){
Return this. age;
};
Var myMammal = new Mammal ('herb the mammal ');
Var name = myMammal. get_name (); // you can access the name attribute.
// Var Cat = function (name ){
// This. name = name;
// This. saying = 'meow ';
//};
// Cat. prototype = new Mammal ();
// Cat. prototype. purr = function (n ){
// Var I, s = '';
// For (I = 0; I <n; I ++ ){
// If (s ){
// S + = '-';
//}
// S + = 'R ';
//}
// Return s;
//};
// Cat. prototype. get_name = function (){
// Return this. says () + ''+ this. name +'' + this. says ();
//};
// In addition to the specific attributes, you can still access the private attributes.

// Define the inherits method to inherit pseudo classes
Function. method ('explores', function (Parent ){
This. prototype = new Parent ();
Return this;
});
Var Cat = function (name ){
This. name = name;
This. saying = 'meow ';
}. Inherits (Mammal). method ('purr', function (n ){
Var I, s = '';
For (I = 0; I <n; I ++ ){
If (s ){
S + = '-';
}
S + = 'R ';
}
Return s;
}). Method ('get _ name', function (){
Return this. says () + ''+ this. name +'' + this. says ();
});
Var myCat = new Cat ('henrietta ');
Var says = myCat. says (); // 'meow'
Var purr = myCat. purr (5); // 'r-r-R'
Var name = myCat. get_name (); // 'meow Henrietta meow'
Var name1 = myCat. name;
// Var age1 = mycat. get_age (); // The parent class method cannot be accessed
Document. writeln ('<br> ');
Document. writeln (says + ''+ purr +'' + name + ''+ name1); // dimension
// Create an array containing 10 zeros
Array. dim = function (dimension, initial ){
Var a = [], I;
For (I = 0; I <dimension; I + = 1 ){
A [I] = initial;
}
Return;
}
Var myArray = Array. dim (10, 0 );

// Add a Matrix Function to Array
Array. matrix = function (m, n, initial ){
Var a, I, j, mat = [];
For (I = 0; I <m; I + = 1 ){
A = [];
For (j = 0; j <n; j + = 1 ){
A [j] = initial;
}
Mat [I] =;
}
Return mat;
}
// Fill the 4*4 matrix with 0
Var myMatrix = Array. matrix (4, 4, 0 );
Document. writeln (myMatrix [3] [3]); // 0

// The method used to construct a constant matrix.
Array. identity = function (n ){
Var I, mat = Array. matrix (n, n, 0 );
For (I = 0; I <n; I + = 1 ){
Mat [I] [I] = 1;
}
Return mat;
};
MyMatrix = Array. identity (4 );
Document. writeln (myMatrix [3] [3]); // 1

 

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.