1.class简单应用
function Course ( teacher, course ) {
this . teacher= teacher;
this . course= course;
}
Course . prototype. getCourse = function ( ) {
return ` teacher is ${ this . teacher} ,course: ${ this . course} `
}
const course= new Course ( 'aa' , 'ES6' ) ;
course. getCourse ( )
class Course {
constructor ( teacher, course ) {
this . teacher= teacher;
this . course= course;
}
getCourse ( ) {
return ` teacher is: ${ this . teacher} ,course is ${ this . course} `
}
}
const course = new Course ( 'aa' , 'ES6' ) ;
course. getCourse ( )
2.class的类型
console. log ( typeOf course)
3.class的prototype
function Course ( teacher, course ) {
this . teacher = teacher;
this . course = course;
}
class Course1 {
constructor ( teacher, course ) {
this . teacher = teacher;
this . course = course;
}
getCourse ( ) {
return ` teacher is: ${ this . teacher} , course: ${ this . course} ` ;
}
}
4.class & 函数对象 属性
console. log ( course. hasOwnProperty ( 'teacher' ) ) ;
5.属性定义 构造器 & 顶层定义 两种定义方式
class Course {
constructor ( teacher, course ) {
this . teacher= teacher;
this . course= course;
}
getCourse ( ) {
return ` teacher is : ${ this . teacher} ,course is : $ {this.course} `
}
get teacher ( ) {
return this . teacher
}
set teacher ( val ) {
this . teacher= val
}
}
class Course {
constructor ( teacher, course ) {
this . _teacher= teacher;
this . course= course;
}
getCourse ( ) {
return ` teacher is : ${ this . teacher} ,course is : ${ this . course} `
}
get teacher ( ) {
return this . _teacher
}
}
2. 如何建立一个私有属性
class Course {
constructor ( ) {
this . _teacher= teacher;
let _course = 'es6' ;
this . getCourse = ( ) => {
return this . _course;
}
}
}
class Course {
#course = 'es6' ;
constructor ( teacher, course ) {
this . _teacher = teacher;
}
get course ( ) {
return this . #course;
}
set course ( val ) {
if ( val) {
this . #course = val
}
}
}
class utils {
constructor ( core ) {
this . _main= core;
this . _name= 'my-utils' ;
this . _id= '123'
}
get name ( ) {
return {
... this . _main. fullName,
... {
name: ` utils is ${ this . name} `
}
}
}
get id ( ) {
return {
... this . _main. id,
id: this . _id
}
}
set name ( val ) {
this . _name= val;
}
}
6.静态方法 - 直接挂载在类上的方法无需实例化获取(static)
function Course ( ) {
}
Course. ring = function ( ) {
}
class Course {
constructor ( ) {
}
static ring ( ) {
}
}
Course. ring ( ) ;
7.继承
function Course ( ) {
}
Course. ring = function ( ) {
}
Course . prototype. send = function ( ) {
}
function Child ( ) {
Course . call ( this , 'aa' , 'es6' )
this . . run = function ( ) {
}
Child . prototype= Course . prototype;
class Course {
constructor ( ) {
}
static ring ( ) { }
send ( ) { }
}
class Child extends Course {
constructor ( ) {
super ( 'aa' , es6)
}
run ( ) { }
}