1. Basic
Object-oriented instances
functionLecture (name, teacher) { This. Name =name; This. Teacher =teacher;} Lecture.prototype.display=function () { return This. Teacher + ' is teaching ' + This. Name;//var le = new Lecture (' Math ', ' John Doe ');//Console.log (Le.display ());functionSchedule (lectures) { This. Lectures =lectures;} Schedule.prototype.display=function () { varstr = "; for(vari = 0; I < This. lectures.length; i++) {str+= This. Lectures[i].display () + ' \ n '; } returnstr;}varsc =NewSchedule ([NewLecture (' Yuwen ', ' AAA '),NewLecture (' Shuxue ', ' BBB ')),NewLecture (' Wuli ', ' CCC ') ]; Sc.display ();View Code
Manipulating page Element instances
<!DOCTYPE HTML><HTML><Head><Script>window.onload= function() { varLis=document.getElementsByTagName ('Li'); for (varI= 0; I<lis.length; I++) {Lis[i].style.border= '1px solid #f00'; } varitem1=document.getElementById ('item1'); Item1.parentNode.removeChild (item1);}</Script></Head><Body><ul> <LiID= "Item1">Item1</Li> <LiID= "Item2">Item2</Li> <LiID= "Item3">Item3</Li> <LiID= "Item4">Item4</Li></ul></Body></HTML>View Code
Mouse move-Out event instance
function () { this. Style.backgroundcolor = ' Blue '; } function () { this. Style.backgroundcolor = ' white '; }
2. Object-oriented
The object is the basic unit of JavaScript, and everything in JS is an object. References, scopes, closures, and contexts are described below.
A reference is a pointer to the actual location of the object.
Scope is a valid area of a variable, and in JS the scope is constrained by a function rather than by a block.
A closure indicates that the inner function can reference a variable defined in the outer function that contains it, even if the outer function has been executed.
The context works through the variable this. The variable this always refers to the object where the code is currently located.
JS Object-Oriented programming notes