JavaScript语法比较灵活,并没有严格规定的面向编程的方式。在JavaScript中支持多种实现面向对象编程方式 。
Object Literals 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const  book1 = {    title : 'Book One' ,     author : 'John Doe' ,     year : '2013' ,     getSummary : function  (         return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ;     } }; console .log(book1.getSummary())console .log(Object .values(book1))console .log(Object .keys(book1))
Constructors & This 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function  Book  (title, author, year )     this .title = title;     this .author = author;     this .year = year;       this .getSummery = function  (         return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ;      } } const  book1 = new  Book('Book one' , 'John Doe' , '2013' );const  book2 = new  Book('Book two' , 'John Doe' , '2016' );console .log(book1);
Prototypes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 function  Book  (title, author, year )     this .title = title;     this .author = author;     this .year = year;   } Book.prototype.getSummary = function  (    return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ; } Book.prototype.getAge = function  (     let  years = new  Date .getFullYear() - this .year;     return  `${this .title}  is ${year}  years old` ; } Book.prototype.revise = function  (newYear )      this .year = newYear;     this .revised = true  } const  book1 = new  Book('Book one' , 'John Doe' , '2013' );console .log(book.getSummary());book1.revise('2018' ) console .log(book1);
Inheritance  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 function  Book  (title, author, year )     this .title = title;     this .author = author;     this .year = year;   } Book.prototype.getSummary = function  (    return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ; } function  Magazine (title, author, year, month )     Book.call(this , title, author, year);     this .month = month; } Magazine.prototype = Object .create(Book.prototype); Magazine.prototype.constructor = Magazine; const  mg1 = new  Magazine('Mag One' , 'John Doe' , '2018' , 'Jan' );console .log(mg1);console .log(mg1.getSummary())
Object.create() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 const  bookProtos = {    getSummary : function  (         return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ;     },     getAge : function  (         let  years = new  Date .getFullYear() - this .year;     	return  `${this .title}  is ${year}  years old` ;      } }; const  book2 = Object .create(bookProtos, {    title : {value : 'Book Two' },     autho : {value : 'John Doe' },     year : {value : '2016 ' } }); 
Classes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class  Book      constructor  (title, author, year ) {         this .title = title;         this .author = author;         this .year = year;     }          getSummary (        return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ;      }          getAge (         let  years = new  Date .getFullYear() - this .year;     	return  `${this .title}  is ${year}  years old` ;            }          revise (newYear )     	this .year = newYear;     	 this .revised = true 	     }          static  topBookStore ( 		return  'Barnes & Noble' ;     } } const  book1 = new  Book('Book One' , 'Johe Doe' , '2013' );console .log(book1)console .log(Book.topBookStore())
SubClasses 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class  Book      constructor  (title, author, year ) {         this .title = title;         this .author = author;         this .year = year;     }          getSummary (        return  `${this .title}  was writter by ${this .author}  in ${this .year} ` ;      }     } class  Magazine  extends  Book ()     constructor (title, author, year, month )         super (title, author,year);         this .month = month;     } } const  mg1 = new  Magazine('Mag One' , 'John Doe' , '2018' , 'Jan' ); console .log(mg1);console .log(mg1.getSummary());