《学习javascript数据结构与算法》类——实例

// 构造一个类
function Book(title, pages, isbn){
    this.title=title;
    this.pages=pages;
    this.isbn=isbn;
    //下2.
    // this.func2=function(){
    //     console.log(222);
    // }
}
// 新建一个类实例
var book=new Book('你不知道的JS', '50', '0001');
// 输出信息
console.log('书名:'+book.title);
console.log('页数:'+book.pages);
console.log('编号:'+book.isbn);

  

 

 

// 更改信息
book.title='数据结构与算法';
console.log('新名字:'+book.title);

  

 

 

// 给类声明函数
// 1.prototype 属性使您有能力向对象添加属性和方法
Book.prototype.func=function(){
    console.log(111);
}
book.func();

  

 

 

// 2.见上方注释  内部定义函数
book.func2();

  

 

 

源码:

// 构造一个类
function Book(title, pages, isbn){
    this.title=title;
    this.pages=pages;
    this.isbn=isbn;
    this.func2=function(){
        console.log(222);
    }
}
// 新建一个类实例
var book=new Book('你不知道的JS', '50', '0001');
// 输出信息
console.log('书名:'+book.title);
console.log('页数:'+book.pages);
console.log('编号:'+book.isbn);
// 更改信息
book.title='数据结构与算法';
console.log('新名字:'+book.title);
// 给类声明函数
// 1.prototype 属性使您有能力向对象添加属性和方法
Book.prototype.func=function(){
    console.log(111);
}
book.func();
// 2.见上方注释  内部定义函数
book.func2();

  

posted @ 2020-07-22 19:20  New7  阅读(181)  评论(0)    收藏  举报