JS Object-Oriented programming notes

Source: Internet
Author: User

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

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.