设计模式知识连载(2)---封装_1

来源:互联网 发布:免费期刊文献资源 知乎 编辑:程序博客网 时间:2024/05/16 18:15
<body><script type="text/javascript">    /**     *  1、创建一个类     */    // var Book = function (id, name, price) {    //  this.id = id ;    //  this.name = name ;    //  this.price = price ;     // }    // // 为这个类添加一个展示的方法(dispaly)    // // 1、一一为原型对象属性赋值    // // Book.prototype.display = function() {    // //   console.log('这是方式一:一一为原型对象属性赋值') ;    // // } ;    // // Book.prototype.use = function() {    // //   console.log('这是方式一:一一为原型对象属性赋值') ;    // // } ;    // // 2、将一个对象赋值给类的原型对象    // Book.prototype = {    //  display : function(){    //      console.log('这是方式二:这是将一个对象赋值给类的原型对象') ;    //  },    //  use : function() {    //      console.log('这是方式二:这是将一个对象赋值给类的原型对象') ;    //  }    // }    // var book1 = new Book(01, 'JavaScript设计模式', 99) ;    // console.log('book1:', book1) ;    // console.log('book1.id:', book1.id) ;    // console.log('book1.name:', book1.name) ;    // console.log('book1.price:', book1.price) ;    // console.log('book1.display:', book1.display) ;    // console.log('book1.use:', book1.use) ;    /**     *  2、     *  私有属性和私有方法,特权方法,     *  对象公有属性和对象公有方法,构造器     */    var Book = function(id, name, price) {        // 私有属性        var num = 1 ;        // 私有方法        function checkId() {            console.log('执行了私有方法checkId()') ;        }        // 特权方法        this.getName = function() {            return num ;            console.log('执行了特权方法里的getName()方法') ;        } ;        this.getPrice = function() {            console.log('执行了特权方法里的getPrice()方法') ;        } ;        this.setName = function(name) {            this.name = name ;            console.log('执行了特权方法里的setName()方法') ;        } ;        this.setPrice = function(price) {            this.price = price ;            console.log('执行了特权方法里的setPrice()方法') ;        } ;        // 对象公有属性        this.id = id ;        this.name = name ;        this.price = price ;        // 对象公有方法        this.copy = function() {} ;        // 构造器        // this.setName(name) ;        // this.setPrice(price) ;        // this.setName('JavaScript入门') ;   // ---这里调用了,外面实例就不需要调用book2.setName('JavaScript入门') ;        // this.setPrice(299) ;             // ---这里调用了,外面实例就不需要调用book2.setPrice(299) ;    }    // 类静态公有属性(对象不能访问)    Book.isChinese = true ;    // 类静态公有方法(对象不能访问)    Book.resetTime = function() {        console.log('这是类静态公有方法:resetTime()') ;    } ;    Book.prototype = {        // 公有属性        isJSBook : false,        // 公有方法        display : function() {            console.log('执行了prototype里的公有方法:display()') ;        }    } ;    var book2 = new Book(02, 'JavaScript设计模式', 99) ;    console.log('book2:', book2) ;    console.log('私有属性num:', book2.num) ;                // undefined    console.log('私有方法checkId:', book2.checkId ) ;       // undefined    console.log('类静态公有属性:', book2.isChinese) ;      // undefined    console.log('类静态公有方法:', book2.resetTime) ;      // undefined    console.log('prototype属性:', book2.isJSBook) ;           console.log('prototype方法:', book2.display) ;    var num = book2.getName() ;    console.log('私有属性num:', num) ;    // book2.setName('JavaScript入门') ;    // book2.setPrice(299) ;    console.log('book2:', book2) ;</script></body>
阅读全文
0 0
原创粉丝点击