Type checking of variables
// Detection Type var str = "Hello world"; if (typeof str== "string") {// use typeof to determine an example of an object type } if (str.constructor==string) { // use constructor properties to determine the type of an object // The constructor property exists in each object and always points to the function that created it }
Another example of using constructor
function User () { } varnew User (); = = User); // true var New me.constructor (); = = You.constructor); // true
//check a list of parameters with a list of variable types strictly functionStrict (Types,args) {if(Types.length! =args.length) {Throw"Invalid number of arguments. Expected "+ Types.length +", received "+ Args.length +" instead. "; } for(vari = 0; i < args.length; i++) { if(args[i].constructor!=Types[i]) { Throw"Invalid argument type. Expected "+ Types[i].name +", received "+ Args[i].constructor.name +" instead. "; }}} strict ([String, Number, Array], ["MyTest", 12, [11, 22, 33]]);
The function of curry
Curry is a technique that transforms a function into a new, simplified (and less accepted) function by filling multiple functions into a function body.
function addgenerator (num) { returnfunction (toadd) { return num + Toadd;} ; } var addfive = addgenerator (5); Alert (Addfive (4) = = 9);
Examples of using privileged methods
function User (name,age) { var year = (new Date ()). getFullYear ()-age ; This function () {// Create a privileged method that can access the year variable, while itself belongs to the publicly accessible return year ;} } var New User (' Angela ', 1989); = = 26);
Examples of dynamic generation methods
functionUser (properties) { for(varIinchproperties) { (function(which) {varp =i; which["Get" + p] =function () { returnProperties[p]; } which["Set" + p] =function(val) {properties[p]=Val; } })( This); } } varuser =NewUser ({name:"Angela", Age:26 }); Alert (User.Name==NULL);//true//name property does not exist because it is a private variable of a Property objectAlert (user.getname () = = "Angela");//true//Use the new GetName method to get this value this function is dynamically generatedUser.setage (22); Alert (User.getage ()= = 22);//trueExamples of static methods
functionUser (name, age) { This. Name =name; This. Age =Age ; } varuser =NewUser (' Angela ', 26); User.cloneuser=function(user) {//Static Methods return NewUser (User.Name, user.age);//Create and return a new user } varCloneuser =User.cloneuser (User); alert (cloneuser.name);//Angela
Browser rendering and sequence of operations
Detect if DOM is available
functionDomready (f) {if(Domready.done) {//If the function is executed immediately after loading returnf (); } if(Domready.timer) {//If we have added a functionDomReady.ready.push (f);//add it to the list of functions you want to execute}Else{addevent (window,' Load ', isdomready);//Add Event Listener} domready.ready= [F];//initializing an array of functions to executeDomready.timer = SetInterval (Isdomready, 13);//Check if the DOM is available as quickly as possible } functionIsdomready () {//Check if DOM is operable if(Domready.done) {//If we can determine that the DOM is available, ignore return false; } //check that several functions and elements are available if(document&&document.getelementsbytagname&&document.getelementbyid&&document.body) {clearinterval (domready.timer);//Stop checking if it is availableDomready.timer =NULL; //executing a waiting function for(vari = 0; i < domReady.ready.length; i++) {domready.ready[i] (); } //Records We've done hereDomready.ready =NULL; Domready.done=true; } }
Xpath
Curry is a technique that transforms a function into a new, simplified (and less accepted) function by filling multiple functions into a function body.
Proficient in JavaScript (reading notes)